zoukankan      html  css  js  c++  java
  • 使用Spring进行统一日志管理 + 统一异常管理

    http://blog.csdn.net/king87130/article/details/8011843原文地址


    统一日志异常实现类:

    1
    package com.pilelot.web.util; 2 3 import org.apache.log4j.Logger; 4 import org.springframework.aop.ThrowsAdvice; 5 import org.springframework.dao.DataAccessException; 6 7 import java.io.IOException; 8 import java.lang.reflect.Method; 9 import java.sql.SQLException; 10 11 /** 12 * 由Spring AOP调用 输出异常信息,把程序异常抛向业务异常 13 * 14 * @author Andy Chan 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 }

     1 自定义业务异常处理类 友好提示:
     2 
     3 package com.pilelot.web.util;
     4 
     5 /**
     6  * 自定义业务异常处理类    友好提示
     7  * @author Andy Chan
     8  *
     9  */
    10 public class BusinessException extends RuntimeException
    11 {
    12     private static final long serialVersionUID = 3152616724785436891L;
    13 
    14     public BusinessException(String frdMessage)
    15     {
    16         super(createFriendlyErrMsg(frdMessage));
    17     }
    18 
    19     public BusinessException(Throwable throwable)
    20     {
    21         super(throwable);
    22     }
    23 
    24     public BusinessException(Throwable throwable, String frdMessage)
    25     {
    26         super(throwable);
    27     }
    28 
    29     private static String createFriendlyErrMsg(String msgBody)
    30     {
    31         String prefixStr = "抱歉,";
    32         String suffixStr = " 请稍后再试或与管理员联系!";
    33 
    34         StringBuffer friendlyErrMsg = new StringBuffer("");
    35 
    36         friendlyErrMsg.append(prefixStr);
    37 
    38         friendlyErrMsg.append(msgBody);
    39 
    40         friendlyErrMsg.append(suffixStr);
    41 
    42         return friendlyErrMsg.toString();
    43     }
    统一日志处理实现类:
    
    package com.pilelot.web.util;
    
    import org.aopalliance.intercept.MethodInterceptor;
    import org.aopalliance.intercept.MethodInvocation;
    import org.apache.log4j.Logger;
    
    /**
     * Spring 统一日志处理实现类
     * @author Andy Chan
     * 
     */
    public class LogInterceptor implements MethodInterceptor
    {
    
        public Object invoke(MethodInvocation invocation) throws Throwable
        {
            Logger loger = Logger.getLogger(invocation.getClass());
    
            loger.info("--Log By Andy Chan -----------------------------------------------------------------------------");
            loger.info(invocation.getMethod() + ":BEGIN!--(Andy ChanLOG)");// 方法前的操作
            Object obj = invocation.proceed();// 执行需要Log的方法
            loger.info(invocation.getMethod() + ":END!--(Andy ChanLOG)");// 方法后的操作
            loger.info("-------------------------------------------------------------------------------------------------");
    
            return obj;
        }
    
    }
    View Code
    
    
    
    44
    Spring配置文件添加:
    
    
        <!-- Spring 统一日志处理   LogInterceptor拦截器 配置 -->   
        <bean id="logLnterceptor" class="com.pilelot.web.util.LogInterceptor"/>
        <!-- Spring 统一异常处理  ExceptionAdvisor配置 -->
        <bean id="exceptionHandler" class="com.pilelot.web.util.ExceptionAdvisor"></bean>
        
            <!-- Bean自动代理处理器 配置-->  
        <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator" >
           <property name="beanNames">
            <list>    <!-- 配置需要进行日志记录的Service和Dao -->
                 <value>commonDao</value>
                         <!-- 配置所有Service结尾命名的Bean,即所有Service层的类都要经过exceptionHandler异常处理类 --> 
                <value>*Service</value>  <!-- Service层的Bean ID 命名要以Service结尾 -->
            </list>
           </property>
           <property name="interceptorNames">
            <list>
                 <value>exceptionHandler</value>
                 <value>logLnterceptor</value>
                 <!--<value>transactionInterceptor</value>-->
            </list>
           </property>
        </bean>
    <!-- ——————————————————Spring 统一日志处理 + 统一异常处理  配置结束—————————————悲伤的分隔线—————————— -->
    View Code
    
    
    
     }


  • 相关阅读:
    Kali视频学习21-25
    20159315《网络攻防实践》第六周学习总结
    Kali视频学习16-20
    20159315《网络攻防实践》第五周学习总结
    一个PE文件的逆向分析
    一个好玩的CTF题
    对于安卓锁屏中知识点小结
    安卓系统安全措施
    安卓防逆向、防动态分析、渗透测试及加固
    安卓组件漏洞防护注意事项
  • 原文地址:https://www.cnblogs.com/leonlee/p/5379297.html
Copyright © 2011-2022 走看看