Spring启动容器初始化Listener
package com.companyName.dhm.common.uif.system;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.companyName.dhm.common.uif.service.ServiceFactory;
/**
* Spring启动容器初始化Listener
* <p>Copyright: Copyright (c) 2009 <p>
* <p>Company: companyName</p>
* @author
* @version 1.0
*/
public class StartupListener extends ContextLoaderListener
implements ServletContextListener {
/**
* 初始化
*/
public void contextInitialized(ServletContextEvent event) {
super.contextInitialized(event);
ServletContext context = event.getServletContext();
setupContext(context);
}
/**
* 设置Spring容器上下文
* @param context
*/
public static void setupContext(ServletContext context) {
ApplicationContext ctx =
WebApplicationContextUtils.getRequiredWebApplicationContext(context);
ServiceFactory.init(ctx);
}
}
package com.companyName.dhm.common.uif.service;
import org.springframework.context.ApplicationContext;
import com.companyName.dhm.common.uif.exception.AppException;
import com.companyName.dhm.common.uif.exception.AppSystemException;
import com.companyName.dhm.common.uif.exception.ExceptionConstants;
/**
* Service工厂类ͬ��ʵ����
* <p>Copyright: Copyright (c) 2009 <p>
* <p>Company: companyName</p>
* @author
* @version 1.0
*/
public class ServiceFactory {
/**
* Spring上下文容器
*/
private static ApplicationContext ctx = null;
/**
* 初始化
* @param context
*/
public static void init(ApplicationContext context) {
ctx = context;
}
/**
* 获取业务对象
* @param serviceName
* @return
* @throws AppException
*/
public static Service getService(String serviceName) throws AppException {
if (ctx == null) {
throw new AppSystemException("创建业务对象失败!",
ExceptionConstants.SYS_GENERAL_ERR);
}
return (Service) ctx.getBean(serviceName);
}
public static ApplicationContext getCtx(){
return ctx;
}
}
package com.companyName.dhm.common.uif.exception;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.io.PrintStream;
import java.io.PrintWriter;
/**
* 异常抽象类ͬ��ʵ����
* <p>Copyright: Copyright (c) 2009 <p>
* <p>Company: companyName</p>
* @author
* @version 1.0
*/
public abstract class AppException extends Exception {
private int errorCode = ExceptionConstants.SUCCESS;
private String exceptionId;
protected Throwable cause;
public AppException(int errorCode) {
super();
this.errorCode = errorCode;
genExceptionId();
}
public AppException(String message, int errorCode) {
super(message);
this.errorCode = errorCode;
genExceptionId();
}
public AppException(Throwable cause, int errorCode) {
super(cause);
this.cause = cause;
this.errorCode = errorCode;
genExceptionId();
}
public AppException(String message, Throwable cause, int errorCode) {
super(message, cause);
this.errorCode = errorCode;
this.cause = cause;
genExceptionId();
}
public int getErrorCode() {
return errorCode;
}
public void setErrorCode(int errorCode) {
this.errorCode = errorCode;
}
public void genExceptionId() {
String idFormat = "yyMMddHHmmssS";
SimpleDateFormat sdf = new SimpleDateFormat(idFormat);
this.exceptionId = sdf.format(new Date());
}
public String getExceptionId() {
return exceptionId;
}
public void setExceptionId(String exceptionId) {
this.exceptionId = exceptionId;
}
public void printStackTrace(PrintStream s) {
s.println("<app_exception>\r\n\t<exception_id>" + exceptionId +
"</exception_id>\r\n");
s.println("\t<exception-localized-message>" + "ErrorCode=" +
getErrorCode() + " LocalizedMessage=" +
getLocalizedMessage() +
"</exception-localized-message>\r\n\t<exception_stack_trace>");
super.printStackTrace(s);
s.println("\t</exception_stack_trace>\r\n</app_exception>");
}
public void printStackTrace(PrintWriter s) {
s.println("<app_exception>\r\n\t<exception_id>" + exceptionId +
"</exception_id>\r\n");
s.println("\t<exception-localized-message>" + "ErrorCode=" +
getErrorCode() + " LocalizedMessage=" +
getLocalizedMessage() +
"</exception-localized-message>\r\n\t<exception_stack_trace>");
super.printStackTrace(s);
s.println("\t</exception_stack_trace>\r\n</app_exception>");
}
}
package com.companyName.dhm.common.uif.exception;
/**
* 系统异常ͬ��ʵ����
* <p>Copyright: Copyright (c) 2009 <p>
* <p>Company: companyName</p>
* @author
* @version 1.0
*/
public class AppSystemException extends AppException {
public AppSystemException(int errorCode) {
super(errorCode);
}
public AppSystemException(String message, int errorCode) {
super(message, errorCode);
}
public AppSystemException(Throwable cause, int errorCode) {
super(cause, errorCode);
}
public AppSystemException(String message, Throwable cause, int errorCode) {
super(message, cause, errorCode);
}
}
package com.companyName.dhm.common.uif.exception;
/**
* 异常常量类ͬ��ʵ����
* <p>Copyright: Copyright (c) 2009 <p>
* <p>Company: companyName</p>
* @author
* @version 1.0
*/
public class ExceptionConstants {
public static final int SUCCESS = 0;
public static final int SYS_GENERAL_ERR = 1;
public static final int DB_COMMON_ERR = 1001;
public static final int DB_CONN_ERR = 1002;
}
package com.companyName.dhm.common.uif.system;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.companyName.dhm.common.uif.service.ServiceFactory;
/**
* Spring启动容器初始化Listener
* <p>Copyright: Copyright (c) 2009 <p>
* <p>Company: companyName</p>
* @author
* @version 1.0
*/
public class StartupListener extends ContextLoaderListener
implements ServletContextListener {
/**
* 初始化
*/
public void contextInitialized(ServletContextEvent event) {
super.contextInitialized(event);
ServletContext context = event.getServletContext();
setupContext(context);
}
/**
* 设置Spring容器上下文
* @param context
*/
public static void setupContext(ServletContext context) {
ApplicationContext ctx =
WebApplicationContextUtils.getRequiredWebApplicationContext(context);
ServiceFactory.init(ctx);
}
}
package com.companyName.dhm.common.uif.service;
import org.springframework.context.ApplicationContext;
import com.companyName.dhm.common.uif.exception.AppException;
import com.companyName.dhm.common.uif.exception.AppSystemException;
import com.companyName.dhm.common.uif.exception.ExceptionConstants;
/**
* Service工厂类ͬ��ʵ����
* <p>Copyright: Copyright (c) 2009 <p>
* <p>Company: companyName</p>
* @author
* @version 1.0
*/
public class ServiceFactory {
/**
* Spring上下文容器
*/
private static ApplicationContext ctx = null;
/**
* 初始化
* @param context
*/
public static void init(ApplicationContext context) {
ctx = context;
}
/**
* 获取业务对象
* @param serviceName
* @return
* @throws AppException
*/
public static Service getService(String serviceName) throws AppException {
if (ctx == null) {
throw new AppSystemException("创建业务对象失败!",
ExceptionConstants.SYS_GENERAL_ERR);
}
return (Service) ctx.getBean(serviceName);
}
public static ApplicationContext getCtx(){
return ctx;
}
}
package com.companyName.dhm.common.uif.exception;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.io.PrintStream;
import java.io.PrintWriter;
/**
* 异常抽象类ͬ��ʵ����
* <p>Copyright: Copyright (c) 2009 <p>
* <p>Company: companyName</p>
* @author
* @version 1.0
*/
public abstract class AppException extends Exception {
private int errorCode = ExceptionConstants.SUCCESS;
private String exceptionId;
protected Throwable cause;
public AppException(int errorCode) {
super();
this.errorCode = errorCode;
genExceptionId();
}
public AppException(String message, int errorCode) {
super(message);
this.errorCode = errorCode;
genExceptionId();
}
public AppException(Throwable cause, int errorCode) {
super(cause);
this.cause = cause;
this.errorCode = errorCode;
genExceptionId();
}
public AppException(String message, Throwable cause, int errorCode) {
super(message, cause);
this.errorCode = errorCode;
this.cause = cause;
genExceptionId();
}
public int getErrorCode() {
return errorCode;
}
public void setErrorCode(int errorCode) {
this.errorCode = errorCode;
}
public void genExceptionId() {
String idFormat = "yyMMddHHmmssS";
SimpleDateFormat sdf = new SimpleDateFormat(idFormat);
this.exceptionId = sdf.format(new Date());
}
public String getExceptionId() {
return exceptionId;
}
public void setExceptionId(String exceptionId) {
this.exceptionId = exceptionId;
}
public void printStackTrace(PrintStream s) {
s.println("<app_exception>\r\n\t<exception_id>" + exceptionId +
"</exception_id>\r\n");
s.println("\t<exception-localized-message>" + "ErrorCode=" +
getErrorCode() + " LocalizedMessage=" +
getLocalizedMessage() +
"</exception-localized-message>\r\n\t<exception_stack_trace>");
super.printStackTrace(s);
s.println("\t</exception_stack_trace>\r\n</app_exception>");
}
public void printStackTrace(PrintWriter s) {
s.println("<app_exception>\r\n\t<exception_id>" + exceptionId +
"</exception_id>\r\n");
s.println("\t<exception-localized-message>" + "ErrorCode=" +
getErrorCode() + " LocalizedMessage=" +
getLocalizedMessage() +
"</exception-localized-message>\r\n\t<exception_stack_trace>");
super.printStackTrace(s);
s.println("\t</exception_stack_trace>\r\n</app_exception>");
}
}
package com.companyName.dhm.common.uif.exception;
/**
* 系统异常ͬ��ʵ����
* <p>Copyright: Copyright (c) 2009 <p>
* <p>Company: companyName</p>
* @author
* @version 1.0
*/
public class AppSystemException extends AppException {
public AppSystemException(int errorCode) {
super(errorCode);
}
public AppSystemException(String message, int errorCode) {
super(message, errorCode);
}
public AppSystemException(Throwable cause, int errorCode) {
super(cause, errorCode);
}
public AppSystemException(String message, Throwable cause, int errorCode) {
super(message, cause, errorCode);
}
}
package com.companyName.dhm.common.uif.exception;
/**
* 异常常量类ͬ��ʵ����
* <p>Copyright: Copyright (c) 2009 <p>
* <p>Company: companyName</p>
* @author
* @version 1.0
*/
public class ExceptionConstants {
public static final int SUCCESS = 0;
public static final int SYS_GENERAL_ERR = 1;
public static final int DB_COMMON_ERR = 1001;
public static final int DB_CONN_ERR = 1002;
}