用aspectJ 做了一次日誌

1、

Java代碼  收藏代碼
  1. <aspectj.version>1.7.4.RELEASE</aspectj.version>  
  2.   
  3.     <dependency>  
  4.             <groupId>org.aspectj</groupId>  
  5.             <artifactId>aspectjweaver</artifactId>  
  6.             <version>${aspectj.version}</version>  
  7.         </dependency>  
  8.           
  9.         <dependency>  
  10.             <groupId>org.aspectj</groupId>  
  11.             <artifactId>aspectjrt</artifactId>  
  12.             <version>${aspectj.version}</version>  
  13.         </dependency>  

 注意:如果JDK1.7的 必須這裏也是1.7+

 

 

2、

Java代碼  收藏代碼
  1. <aop:config proxy-target-class="true"></aop:config>  
  2.     <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">  
  3.         <property name="securityManager" ref="securityManager"/>  
  4.     </bean>  
  5.       
  6.     <aop:aspectj-autoproxy proxy-target-class="true"/>  

 注意:下載 必須在spring-mvc.xml裏面,且有兩個aop配置,下面那個是必須的,上面那個可能不是必須的(上面那個應該是spring aop的,如果有aspectJ了,可以不需要)

 

3、AOP類

 

Java代碼  收藏代碼
  1. package com.kingen.aop;  
  2.   
  3. import java.lang.reflect.Method;  
  4.   
  5. import javax.servlet.http.HttpServletRequest;  
  6. import javax.transaction.Transactional;  
  7.   
  8. import org.aspectj.lang.JoinPoint;  
  9. import org.aspectj.lang.annotation.AfterReturning;  
  10. import org.aspectj.lang.annotation.AfterThrowing;  
  11. import org.aspectj.lang.annotation.Aspect;  
  12. import org.aspectj.lang.annotation.Pointcut;  
  13. import org.slf4j.Logger;  
  14. import org.slf4j.LoggerFactory;  
  15. import org.springframework.stereotype.Component;  
  16. import org.springframework.util.Assert;  
  17. import org.springframework.web.context.request.RequestContextHolder;  
  18. import org.springframework.web.context.request.ServletRequestAttributes;  
  19.   
  20. import com.kingen.bean.Log;  
  21. import com.kingen.bean.User;  
  22. import com.kingen.service.log.LogService;  
  23. import com.kingen.util.Constants;  
  24. import com.kingen.util.DateUtils;  
  25. import com.kingen.util.SpringContextHolder;  
  26. import com.kingen.util.StringUtils;  
  27. import com.kingen.util.UserUtils;  
  28.   
  29. /** 
  30.  * 日誌AOP 
  31.  *  
  32.  * @author wj 
  33.  * @date 2016-11-16 
  34.  * 
  35.  */  
  36. @Aspect  
  37. @Component  
  38. public class LogAOP {  
  39.   下載 
  40.     private static LogService logService = SpringContextHolder.getBean(LogService.class);  
  41.   
  42.     // 本地異常日誌記錄對象  
  43.     private Logger logger = LoggerFactory.getLogger(getClass());  
  44.   
  45.     /** 
  46.      * 在所有標註@LogAnnotation的地方切入 
  47.      *  
  48.      * @param joinPoint 
  49.      */  
  50.     @Pointcut("@annotation(com.kingen.aop.LogAnnotation)")  
  51.     public void logAspect() {  
  52.     }  
  53.   
  54.     // @Around(value = "aApplogic() && @annotation(annotation) &&args(object,..)  
  55.     // ", argNames = "annotation,object")  
  56.     // public Object around(ProceedingJoinPoint pj,  
  57.     // LogAnnotation annotation, Object object) throws Throwable {  
  58.     // System.out.println("moduleName:"+annotation.moduleName());  
  59.     // System.out.println("option:"+annotation.option());  
  60.     // pj.proceed();  
  61.     // return object;  
  62.     // }  
  63.   
  64.     /** 
  65.      * 前置通知 用於攔截Controller層記錄用戶的操作 
  66.      *  
  67.      *  joinPoint 
  68.      *            切點 
  69.      * @throws Exception 
  70.      */  
  71. //  @Around(value = "logAspect() && @annotation(annotation) &&args(object,..) ", argNames = "annotation,object")  
  72. //  public void doAround(ProceedingJoinPoint joinPoint, LogAnnotation annotation, Object object) {  
  73.     //用@Around 會導致controller不執行,不返回頁面  
  74.       
  75. //   @After(value = "logAspect() && @annotation(annotation) &&args(object,..) ", argNames = "annotation,object")  
  76. //   public void doAfter(JoinPoint joinPoint, LogAnnotation annotation, Object object) {  
  77.       
  78.      @AfterReturning(value = "logAspect() && @annotation(annotation) &&args(object,..) ", argNames = "", returning = "retVal")  
  79.      public void doAfterReturning(JoinPoint joinPoint, LogAnnotation annotation, Object object,  String retVal) {  
  80.   
  81.         HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())  
  82.                 .getRequest();  
  83.         try {  
  84.             // String title = getAnnotationValue(joinPoint);  
  85.             String title = getAnnotationValue(annotation);  
  86.             saveLog(request, title);  
  87.         } catch (Exception e) {  
  88.             e.printStackTrace();  
  89.             // 記錄本地異常日誌  
  90.             logger.error("==異常通知異常==");  
  91.             logger.error("異常信息:{}", e.getMessage());  
  92.         }  
  93.     }  下載 
  94.   
  95.     /** 
  96.      * 異常通知 用於攔截service層記錄異常日誌 
  97.      *  
  98.      *  joinPoint 
  99.      *  e 
  100.      */  
  101.     // 方法  catch住異常的話,這裏執行不到  
  102. //  @AfterThrowing(pointcut = "logAspect()", throwing = "e")  
  103.     @AfterThrowing(value = "logAspect() && @annotation(annotation) &&args(..) " , throwing = "e")  
  104.     public void doAfterThrowing(JoinPoint joinPoint,  LogAnnotation annotation, Exception e) {  
  105.         HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())  
  106.                 .getRequest();  
  107.   
  108.         try {  
  109.   
  110. //          String title = getAnnotationValue(joinPoint);  
  111.             String title = getAnnotationValue(annotation);  
  112.             saveLog(request, title, e);  
  113.         } catch (Exception ex) {  
  114.             // 記錄本地異常日誌  
  115.             logger.error("==異常通知異常==");  
  116.             logger.error("異常信息:{}", ex.getMessage());  
  117.         }  
  118.   
  119.     }  
  120.   
  121.     public static void saveLog(HttpServletRequest request, String title) {  
  122.         saveLog(request, title, null);  
  123.     }  
  124.   
  125.     /** 
  126.      * 保存日誌 
  127.      */  
  128.     @Transactional  
  129.     public static void saveLog(HttpServletRequest request, String title, Exception ex) {  
  130.         User user = UserUtils.getCurrentUser();  
  131.         if (user != null && user.getUserId() != null) {  
  132.             Log log = new Log();  
  133.             log.setCreateDate(DateUtils.getDateTime());  
  134.             log.setTitle(title);  
  135.             log.setType(ex == null ? Log.TYPE_ACCESS : Log.TYPE_EXCEPTION);  
  136.             log.setRemoteAddr(StringUtils.getRemoteAddr(request));  
  137.             log.setUserAgent(user.getUsername());  
  138.             // log.setUserAgent(request.getHeader("user-agent"));  
  139.             log.setRequestUri(request.getRequestURI());  
  140.             log.setParams(request.getParameterMap());  
  141.             // 如果有異常,設置異常信息  
  142.             log.setException(ex == null ? null : ex.getMessage());  
  143. //          log.setException(ex == null ? null : Exceptions.getStackTraceAsString(ex));  
  144.             log.setStatus(ex == null ? Constants.StatusEnum.Success.getIndex() : Constants.StatusEnum.Fail.getIndex());  
  145.             // log.setMethod(request.getMethod());  
  146.             // 異步保存日誌  
  147.             // new SaveLogThread(log, handler, ex).start();  
  148.             logService.saveLog(log);  
  149.         }  下載 
  150.     }  
  151.   
  152.     /** 
  153.      * 獲取註解中對方法的描述信息 用於Controller層註解 
  154.      *  
  155.      * @param joinPoint 
  156.      *            切點 
  157.      * @return 方法描述 
  158.      * @throws Exception 
  159.      */  
  160.     @Deprecated  
  161.     public static String getAnnotationValue(JoinPoint joinPoint) throws Exception {  
  162.         String targetName = joinPoint.getTarget().getClass().getName();  
  163.         String methodName = joinPoint.getSignature().getName();  
  164.         Object[] arguments = joinPoint.getArgs();  
  165.         Class targetClass = Class.forName(targetName);  
  166.         Method[] methods = targetClass.getMethods();  
  167.         String description = "";  
  168.         for (Method method : methods) {  
  169.             if (method.getName().equals(methodName)) {  
  170.                 Class[] clazzs = method.getParameterTypes();  
  171.                 if (clazzs.length == arguments.length) {  
  172.                     String moduleName = method.getAnnotation(LogAnnotation.class).moduleName();  
  173.                     String option = method.getAnnotation(LogAnnotation.class).option();  
  174.                     Assert.hasText(moduleName, "模塊名字不應爲空");  
  175.                     Assert.hasText(option, "操作名字不應爲空");  
  176.                     description = moduleName + "-" + option;  
  177.                     break;  
  178.                 }  
  179.             }  
  180.         }  
  181.         return description;  
  182.     }  
  183.   
  184.     public static String getAnnotationValue(LogAnnotation anno) throws Exception {  
  185.         String moduleName = anno.moduleName();  
  186.         String option = anno.option();  
  187.         Assert.hasText(moduleName, "模塊名字不應爲空");  
  188.         Assert.hasText(option, "操作名字不應爲空");  
  189.         String description = moduleName + "-" + option;  
  190.         return description;  
  191.     }  
  192. }  

 注意這裏 @After和 @AfterReturning的區別,總的來說,就是 @After 是無論如何都會執行的,不管有沒有異常拋出(這樣會導致,在有異常的時候,記錄兩次日誌,after一次、throwing一次);@AfterReturning 在有異常的情況下,不會執行到,因爲沒有renturn,在retrun之前就throw了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章