今天睡了一觉,起来突然想上来写点关于spring的东西,说动手就动手,买上开始写,首先声明,这个是用在spring2.0上的,不过估计1.2也能用,因为配置文件方面我还是用的spring1.0的配置方法,aop的那种配置方式还没找到怎么写。
代码是从我的项目中直接拷贝出来的,想要用自己改一下就行了。
不说了先上配置文件代码
<bean id="exceptionHandler" class="com.MobileRDA.ExceptionAdvisor"></bean><!--异常处理类声明--> <bean id="BeanNameAutoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"><!--就是通过它来配置我们的spring异常处理--> <property name="beanNames"> <list> <!-- springBean的命名以Service结尾的都走这个异常处理类 如这个 我的beanID就是用UserLoginService 这个命名规则就符合了我的beanNames的命名规则。 <bean id="UserLoginService" class="com.MobileRDA.services.impl.UserLoginServiceImpl"> <property name="xtm08DAO" ref="Xtm08DAO"></property> <property name="xtm081DAO" ref="Xtm081DAO"></property> </bean> --> <value>*Service</value> </list> </property> <property name="interceptorNames"> <value>exceptionHandler</value><!--异常执行的bean,与上面声明的bean对应--> </property> </bean>
下面贴上我的处理bean的源码
package com.MobileRDA;
import org.springframework.aop.ThrowsAdvice;
import org.springframework.dao.DataAccessException;
import java.io.IOException;
import java.lang.reflect.Method;
import java.sql.SQLException;
public class ExceptionAdvisor implements ThrowsAdvice {
public void afterThrowing(Method method, Object[] args, Object target, Exception ex) throws Throwable {
//在后台中输出错误异常异常信息,可以通过log4j输出,自己修改即可。
System.out.println("*************************************");
System.out.println("Error happened in class: " + target.getClass().getName());
System.out.println("Error happened in method: " + method.getName());
for (int i = 0; i < args.length; i++) {
System.out.println("arg[" + i + "]: " + args[i]);
}
System.out.println("Exception class: " + ex.getClass().getName());
System.out.println("ex.getMessage():"+ex.getMessage());
ex.printStackTrace();
System.out.println("*************************************");
//在这里判断异常,根据不同的异常返回错误。
if(ex.getClass().equals(DataAccessException.class)){
ex.printStackTrace();
throw new BusinessException("数据库操作失败!");
}else if(ex.getClass().toString().equals(NullPointerException.class.toString())) {
ex.printStackTrace();
throw new BusinessException("调用了未经初始化的对象或者是不存在的对象!");
}else if(ex.getClass().equals(IOException.class)) {
ex.printStackTrace();
throw new BusinessException("IO异常!");
}else if(ex.getClass().equals(ClassNotFoundException.class)) {
ex.printStackTrace();
throw new BusinessException("指定的类不存在!");
}else if(ex.getClass().equals(ArithmeticException.class)) {
ex.printStackTrace();
throw new BusinessException("数学运算异常!");
}else if(ex.getClass().equals(ArrayIndexOutOfBoundsException.class)) {
ex.printStackTrace();
throw new BusinessException("数组下标越界!");
}else if(ex.getClass().equals(IllegalArgumentException.class)) {
ex.printStackTrace();
throw new BusinessException("方法的参数错误!");
}else if(ex.getClass().equals(ClassCastException.class)) {
ex.printStackTrace();
throw new BusinessException("类型强制转换错误!");
}else if(ex.getClass().equals(SecurityException .class)) {
ex.printStackTrace();
throw new BusinessException("违背安全原则异常!");
}else if(ex.getClass().equals(SQLException.class)) {
ex.printStackTrace();
throw new BusinessException("操作数据库异常!");
}else if(ex.getClass().equals(NoSuchMethodError.class)) {
ex.printStackTrace();
throw new BusinessException("方法末找到异常!");
}else if(ex.getClass().equals(InternalError.class)) {
ex.printStackTrace();
throw new BusinessException("Java虚拟机发生了内部错误");
}else{
ex.printStackTrace();
throw new BusinessException("程序内部错误,操作失败!"+ex.getMessage());
}
}
}
通过上面的处理就能按照我们的需要输出所有的异常信息了。
下面是BusinessException的源码
package com.MobileRDA;
public class BusinessException extends RuntimeException {
private static final long serialVersionUID = 3152616724785436891L;
public BusinessException(String frdMessage) {
super(createFriendlyErrMsg(frdMessage));
}
public BusinessException(Throwable throwable) {
super(throwable);
}
public BusinessException(Throwable throwable, String frdMessage) {
super(throwable);
}
private static String createFriendlyErrMsg(String msgBody){
String prefixStr = "抱歉,";
String suffixStr = " 请稍后再试或与管理员联系!";
StringBuffer friendlyErrMsg = new StringBuffer("");
friendlyErrMsg.append(prefixStr);
friendlyErrMsg.append(msgBody);
friendlyErrMsg.append(suffixStr);
return friendlyErrMsg.toString();
}
}
很简单,基本上都能看懂应该,就不多说了。