spring 全局的异常处理

统一日志异常实现类:html

[java] view plaincopyjava

  1. package com.pilelot.web.util;  web

  2.   

  3. import org.apache.log4j.Logger;  spring

  4. import org.springframework.aop.ThrowsAdvice;  sql

  5. import org.springframework.dao.DataAccessException;  数据库

  6.   

  7. import java.io.IOException;  apache

  8. import java.lang.reflect.Method;  数组

  9. import java.sql.SQLException;  安全

  10.   

  11. /** app

  12.  * 由Spring AOP调用 输出异常信息,把程序异常抛向业务异常 

  13.  *  

  14.  * @author josephshui

  15.  * 

  16.  */  

  17. public class ExceptionAdvisor implements ThrowsAdvice  

  18. {  

  19.     public void afterThrowing(Method method, Object[] args, Object target,  

  20.             Exception ex) throws Throwable  

  21.     {  

  22.         // 在后台中输出错误异常异常信息,经过log4j输出。  

  23.         Logger log = Logger.getLogger(target.getClass());  

  24.         log.info("**************************************************************");  

  25.         log.info("Error happened in class: " + target.getClass().getName());  

  26.         log.info("Error happened in method: " + method.getName());  

  27.             for (int i = 0; i < args.length; i++)  

  28.             {  

  29.                 log.info("arg[" + i + "]: " + args[i]);  

  30.             }  

  31.         log.info("Exception class: " + ex.getClass().getName());  

  32.         log.info("ex.getMessage():" + ex.getMessage());  

  33.         ex.printStackTrace();  

  34.         log.info("**************************************************************");  

  35.   

  36.         // 在这里判断异常,根据不一样的异常返回错误。  

  37.         if (ex.getClass().equals(DataAccessException.class))  

  38.         {  

  39.             ex.printStackTrace();  

  40.             throw new BusinessException("数据库操做失败!");  

  41.         } else if (ex.getClass().toString().equals(  

  42.                 NullPointerException.class.toString()))  

  43.         {  

  44.             ex.printStackTrace();  

  45.             throw new BusinessException("调用了未经初始化的对象或者是不存在的对象!");  

  46.         } else if (ex.getClass().equals(IOException.class))  

  47.         {  

  48.             ex.printStackTrace();  

  49.             throw new BusinessException("IO异常!");  

  50.         } else if (ex.getClass().equals(ClassNotFoundException.class))  

  51.         {  

  52.             ex.printStackTrace();  

  53.             throw new BusinessException("指定的类不存在!");  

  54.         } else if (ex.getClass().equals(ArithmeticException.class))  

  55.         {  

  56.             ex.printStackTrace();  

  57.             throw new BusinessException("数学运算异常!");  

  58.         } else if (ex.getClass().equals(ArrayIndexOutOfBoundsException.class))  

  59.         {  

  60.             ex.printStackTrace();  

  61.             throw new BusinessException("数组下标越界!");  

  62.         } else if (ex.getClass().equals(IllegalArgumentException.class))  

  63.         {  

  64.             ex.printStackTrace();  

  65.             throw new BusinessException("方法的参数错误!");  

  66.         } else if (ex.getClass().equals(ClassCastException.class))  

  67.         {  

  68.             ex.printStackTrace();  

  69.             throw new BusinessException("类型强制转换错误!");  

  70.         } else if (ex.getClass().equals(SecurityException.class))  

  71.         {  

  72.             ex.printStackTrace();  

  73.             throw new BusinessException("违背安全原则异常!");  

  74.         } else if (ex.getClass().equals(SQLException.class))  

  75.         {  

  76.             ex.printStackTrace();  

  77.             throw new BusinessException("操做数据库异常!");  

  78.         } else if (ex.getClass().equals(NoSuchMethodError.class))  

  79.         {  

  80.             ex.printStackTrace();  

  81.             throw new BusinessException("方法末找到异常!");  

  82.         } else if (ex.getClass().equals(InternalError.class))  

  83.         {  

  84.             ex.printStackTrace();  

  85.             throw new BusinessException("Java虚拟机发生了内部错误");  

  86.         } else  

  87.         {  

  88.             ex.printStackTrace();  

  89.             throw new BusinessException("程序内部错误,操做失败!" + ex.getMessage());  

  90.         }  

  91.     }  

  92. }  


自定义业务异常处理类 友好提示:

[java] view plaincopy

  1. package com.pilelot.web.util;  

  2.   

  3. /** 

  4.  * 自定义业务异常处理类    友好提示 

  5.  * @author josephshui

  6.  * 

  7.  */  

  8. public class BusinessException extends RuntimeException  

  9. {  

  10.     private static final long serialVersionUID = 3152616724785436891L;  

  11.   

  12.     public BusinessException(String frdMessage)  

  13.     {  

  14.         super(createFriendlyErrMsg(frdMessage));  

  15.     }  

  16.   

  17.     public BusinessException(Throwable throwable)  

  18.     {  

  19.         super(throwable);  

  20.     }  

  21.   

  22.     public BusinessException(Throwable throwable, String frdMessage)  

  23.     {  

  24.         super(throwable);  

  25.     }  

  26.   

  27.     private static String createFriendlyErrMsg(String msgBody)  

  28.     {  

  29.         String prefixStr = "抱歉,";  

  30.         String suffixStr = " 请稍后再试或与管理员联系!";  

  31.   

  32.         StringBuffer friendlyErrMsg = new StringBuffer("");  

  33.   

  34.         friendlyErrMsg.append(prefixStr);  

  35.   

  36.         friendlyErrMsg.append(msgBody);  

  37.   

  38.         friendlyErrMsg.append(suffixStr);  

  39.   

  40.         return friendlyErrMsg.toString();  

  41.     }  

  42. }  


 

 

统一日志处理实现类:

[java] view plaincopy

  1. package com.pilelot.web.util;  

  2.   

  3. import org.aopalliance.intercept.MethodInterceptor;  

  4. import org.aopalliance.intercept.MethodInvocation;  

  5. import org.apache.log4j.Logger;  

  6.   

  7. /** 

  8.  * Spring 统一日志处理实现类 

  9.  * @author josephshui

  10.  *  

  11.  */  

  12. public class LogInterceptor implements MethodInterceptor  

  13. {  

  14.   

  15.     public Object invoke(MethodInvocation invocation) throws Throwable  

  16.     {  

  17.         Logger loger = Logger.getLogger(invocation.getClass());  

  18.   

  19.         loger.info("--Log By Andy Chan -----------------------------------------------------------------------------");  

  20.         loger.info(invocation.getMethod() + ":BEGIN!--(Andy ChanLOG)");// 方法前的操做  

  21.         Object obj = invocation.proceed();// 执行须要Log的方法  

  22.         loger.info(invocation.getMethod() + ":END!--(Andy ChanLOG)");// 方法后的操做  

  23.         loger.info("-------------------------------------------------------------------------------------------------");  

  24.   

  25.         return obj;  

  26.     }  

  27.   

  28. }  

 

Spring配置文件添加:

 

[html] view plaincopy

  1. <!-- Spring 统一日志处理   LogInterceptor拦截器 配置 -->     

  2. <bean id="logLnterceptor" class="com.pilelot.web.util.LogInterceptor"/>  

  3. <!-- Spring 统一异常处理  ExceptionAdvisor配置 -->  

  4. <bean id="exceptionHandler" class="com.pilelot.web.util.ExceptionAdvisor"></bean>  

  5.   

  6.     <!-- Bean自动代理处理器 配置-->    

  7. <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator" >  

  8.    <property name="beanNames">  

  9.     <list>    <!-- 配置须要进行日志记录的Service和Dao -->  

  10.         <value>commonDao</value>  

  11.                 <!-- 配置全部Service结尾命名的Bean,即全部Service层的类都要通过exceptionHandler异常处理类 -->   

  12.         <value>*Service</value>  <!-- Service层的Bean ID 命名要以Service结尾 -->  

  13.     </list>  

  14.    </property>  

  15.    <property name="interceptorNames">  

  16.     <list>  

  17.          <value>exceptionHandler</value>  

  18.          <value>logLnterceptor</value>  

  19.          <!--<value>transactionInterceptor</value>-->  

  20.     </list>  

  21.    </property>  

  22. </bean>

相关文章
相关标签/搜索