国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

springAOP自定義注解方式實(shí)現(xiàn)日志管理

2019-11-14 15:18:14
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

今天繼續(xù)實(shí)現(xiàn)AOP,到這里我個(gè)人認(rèn)為是最靈活,可擴(kuò)展的方式了,就拿日志管理來(lái)說(shuō),用SPRing AOP 自定義注解形式實(shí)現(xiàn)日志管理。廢話不多說(shuō),直接開(kāi)始!!!

關(guān)于配置我還是的再說(shuō)一遍。

 

applicationContext-mvc.xml中要添加的

     <mvc:annotation-driven />
     <!-- 激活組件掃描功能,在包c(diǎn)om.gcx及其子包下面自動(dòng)掃描通過(guò)注解配置的組件 -->
     <context:component-scan base-package="com.gcx" />

  

     <!-- 啟動(dòng)對(duì)@aspectJ注解的支持 -->
     <!-- proxy-target-class等于true是強(qiáng)制使用cglib代理,proxy-target-class默認(rèn)是false,如果你的類實(shí)現(xiàn)了接口 就走JDK代理,如果沒(méi)有,走cglib代理  -->
     <!-- 注:對(duì)于單利模式建議使用cglib代理,雖然JDK動(dòng)態(tài)代理比cglib代理速度快,但性能不如cglib -->

     <!--如果不寫(xiě)proxy-target-class="true"這句話也沒(méi)問(wèn)題-->
     <aop:aspectj-autoproxy proxy-target-class="true"/>

     <!--切面-->
     <bean id="systemLogAspect" class="com.gcx.annotation.SystemLogAspect"></bean>

接下來(lái)開(kāi)始編寫(xiě)代碼。

     創(chuàng)建日志類實(shí)體

  1 public class SystemLog {  2     private String id;  3   4     private String description;  5   6     private String method;  7   8     private Long logType;  9  10     private String requestIp; 11  12     private String exceptioncode; 13  14     private String exceptionDetail; 15  16     private String params; 17  18     private String createBy; 19  20     private Date createDate; 21  22     public String getId() { 23         return id; 24     } 25  26     public void setId(String id) { 27         this.id = id == null ? null : id.trim(); 28     } 29  30     public String getDescription() { 31         return description; 32     } 33  34     public void setDescription(String description) { 35         this.description = description == null ? null : description.trim(); 36     } 37  38     public String getMethod() { 39         return method; 40     } 41  42     public void setMethod(String method) { 43         this.method = method == null ? null : method.trim(); 44     } 45  46     public Long getLogType() { 47         return logType; 48     } 49  50     public void setLogType(Long logType) { 51         this.logType = logType; 52     } 53  54     public String getRequestIp() { 55         return requestIp; 56     } 57  58     public void setRequestIp(String requestIp) { 59         this.requestIp = requestIp == null ? null : requestIp.trim(); 60     } 61  62     public String getExceptioncode() { 63         return exceptioncode; 64     } 65  66     public void setExceptioncode(String exceptioncode) { 67         this.exceptioncode = exceptioncode == null ? null : exceptioncode.trim(); 68     } 69  70     public String getExceptionDetail() { 71         return exceptionDetail; 72     } 73  74     public void setExceptionDetail(String exceptionDetail) { 75         this.exceptionDetail = exceptionDetail == null ? null : exceptionDetail.trim(); 76     } 77  78     public String getParams() { 79         return params; 80     } 81  82     public void setParams(String params) { 83         this.params = params == null ? null : params.trim(); 84     } 85  86     public String getCreateBy() { 87         return createBy; 88     } 89  90     public void setCreateBy(String createBy) { 91         this.createBy = createBy == null ? null : createBy.trim(); 92     } 93  94     public Date getCreateDate() { 95         return createDate; 96     } 97  98     public void setCreateDate(Date createDate) { 99         this.createDate = createDate;100     }101 }
View Code

     編寫(xiě)dao接口

 1 package com.gcx.dao; 2  3 import com.gcx.entity.SystemLog; 4  5 public interface SystemLogMapper { 6     int deleteByPrimaryKey(String id); 7  8     int insert(SystemLog record); 9 10     int insertSelective(SystemLog record);11 12     SystemLog selectByPrimaryKey(String id);13 14     int updateByPrimaryKeySelective(SystemLog record);15 16     int updateByPrimaryKey(SystemLog record);17 }
View Code

    編寫(xiě)service層

 1 package com.gcx.service; 2  3 import com.gcx.entity.SystemLog; 4  5 public interface SystemLogService { 6  7     int deleteSystemLog(String id); 8  9     int insert(SystemLog record);10     11     int insertTest(SystemLog record);12 13     SystemLog selectSystemLog(String id);14     15     int updateSystemLog(SystemLog record);16 }
View Code

   編寫(xiě)service實(shí)現(xiàn)類serviceImpl

 1 package com.gcx.service.impl; 2  3 import javax.annotation.Resource; 4  5 import org.springframework.stereotype.Service; 6  7 import com.gcx.annotation.Log; 8 import com.gcx.dao.SystemLogMapper; 9 import com.gcx.entity.SystemLog;10 import com.gcx.service.SystemLogService;11 12 @Service("systemLogService")13 public class SystemLogServiceImpl implements SystemLogService {14 15     @Resource16     private SystemLogMapper systemLogMapper;17     18     @Override19     public int deleteSystemLog(String id) {20         21         return systemLogMapper.deleteByPrimaryKey(id);22     }23 24     @Override25     26     public int insert(SystemLog record) {27         28         return systemLogMapper.insertSelective(record);29     }30 31     @Override32     public SystemLog selectSystemLog(String id) {33         34         return systemLogMapper.selectByPrimaryKey(id);35     }36 37     @Override38     public int updateSystemLog(SystemLog record) {39         40         return systemLogMapper.updateByPrimaryKeySelective(record);41     }42 43     @Override44     public int insertTest(SystemLog record) {45         46         return systemLogMapper.insert(record);47     }48 49 }
View Code

到這里基本程序編寫(xiě)完畢

下面開(kāi)始自定義注解

 1 package com.gcx.annotation; 2  3 import java.lang.annotation.*; 4  5 @Target({ElementType.PARAMETER, ElementType.METHOD})   6 @Retention(RetentionPolicy.RUNTIME)   7 @Documented   8 public @interface Log { 9 10     /** 要執(zhí)行的操作類型比如:add操作 **/  11     public String OperationType() default "";  12      13     /** 要執(zhí)行的具體操作比如:添加用戶 **/  14     public String operationName() default "";15 }

 

下面編寫(xiě)切面

  1 package com.gcx.annotation;  2   3 import java.lang.reflect.Method;  4 import java.util.Date;  5 import java.util.UUID;  6   7 import javax.annotation.Resource;  8 import javax.servlet.http.HttpServletRequest;  9 import javax.servlet.http.Httpsession; 10  11 import org.aspectj.lang.JoinPoint; 12 import org.aspectj.lang.ProceedingJoinPoint; 13 import org.aspectj.lang.annotation.After; 14 import org.aspectj.lang.annotation.AfterReturning; 15 import org.aspectj.lang.annotation.AfterThrowing; 16 import org.aspectj.lang.annotation.Around; 17 import org.aspectj.lang.annotation.Aspect; 18 import org.aspectj.lang.annotation.Before; 19 import org.aspectj.lang.annotation.Pointcut; 20 import org.slf4j.Logger; 21 import org.slf4j.LoggerFactory; 22 import org.springframework.stereotype.Component; 23  24 import com.gcx.entity.SystemLog; 25 import com.gcx.entity.User; 26 import com.gcx.service.SystemLogService; 27 import com.gcx.util.JsonUtil; 28  29 /** 30  * @author 楊建  31  * @E-mail: email 32  * @version 創(chuàng)建時(shí)間:2015-10-19 下午4:29:05 33  * @desc 切點(diǎn)類  34  */ 35  36 @Aspect 37 @Component 38 public class SystemLogAspect { 39  40     //注入Service用于把日志保存數(shù)據(jù)庫(kù)   41     @Resource  //這里我用resource注解,一般用的是@Autowired,他們的區(qū)別如有時(shí)間我會(huì)在后面的博客中來(lái)寫(xiě) 42     private SystemLogService systemLogService;   43      44     private  static  final Logger logger = LoggerFactory.getLogger(SystemLogAspect. class);   45      46     //Controller層切點(diǎn)   47     @Pointcut("execution (* com.gcx.controller..*.*(..))")   48     public  void controllerAspect() {   49     }   50      51     /**  52      * 前置通知 用于攔截Controller層記錄用戶的操作  53      *  54      * @param joinPoint 切點(diǎn)  55      */  56     @Before("controllerAspect()") 57     public void doBefore(JoinPoint joinPoint) { 58         System.out.println("==========執(zhí)行controller前置通知==============="); 59         if(logger.isInfoEnabled()){ 60             logger.info("before " + joinPoint); 61         } 62     }     63      64     //配置controller環(huán)繞通知,使用在方法aspect()上注冊(cè)的切入點(diǎn) 65       @Around("controllerAspect()") 66       public void around(JoinPoint joinPoint){ 67           System.out.println("==========開(kāi)始執(zhí)行controller環(huán)繞通知==============="); 68           long start = System.currentTimeMillis(); 69           try { 70               ((ProceedingJoinPoint) joinPoint).proceed(); 71               long end = System.currentTimeMillis(); 72               if(logger.isInfoEnabled()){ 73                   logger.info("around " + joinPoint + "/tUse time : " + (end - start) + " ms!"); 74               } 75               System.out.println("==========結(jié)束執(zhí)行controller環(huán)繞通知==============="); 76           } catch (Throwable e) { 77               long end = System.currentTimeMillis(); 78               if(logger.isInfoEnabled()){ 79                   logger.info("around " + joinPoint + "/tUse time : " + (end - start) + " ms with exception : " + e.getMessage()); 80               } 81           } 82       } 83      84     /**  85      * 后置通知 用于攔截Controller層記錄用戶的操作  86      *  87      * @param joinPoint 切點(diǎn)  88      */   89     @After("controllerAspect()")   90     public  void after(JoinPoint joinPoint) {   91    92        /* HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();   93         HttpSession session = request.getSession();  */ 94         //讀取session中的用戶   95        // User user = (User) session.getAttribute("user");   96         //請(qǐng)求的IP   97         //String ip = request.getRemoteAddr(); 98         User user = new User(); 99         user.setId(1);100         user.setName("張三");101         String ip = "127.0.0.1";102          try {  103             104             String targetName = joinPoint.getTarget().getClass().getName();  105             String methodName = joinPoint.getSignature().getName();  106             Object[] arguments = joinPoint.getArgs();  107             Class targetClass = Class.forName(targetName);  108             Method[] methods = targetClass.getMethods();109             String operationType = "";110             String operationName = "";111              for (Method method : methods) {  112                  if (method.getName().equals(methodName)) {  113                     Class[] clazzs = method.getParameterTypes();  114                      if (clazzs.length == arguments.length) {  115                          operationType = method.getAnnotation(Log.class).operationType();116                          operationName = method.getAnnotation(Log.class).operationName();117                          break;  118                     }  119                 }  120             }121             //*========控制臺(tái)輸出=========*//  122             System.out.println("=====controller后置通知開(kāi)始=====");  123             System.out.println("請(qǐng)求方法:" + (joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()")+"."+operationType);  124             System.out.println("方法描述:" + operationName);  125             System.out.println("請(qǐng)求人:" + user.getName());  126             System.out.println("請(qǐng)求IP:" + ip);  127             //*========數(shù)據(jù)庫(kù)日志=========*//  128             SystemLog log = new SystemLog();  129             log.setId(UUID.randomUUID().toString());130             log.setDescription(operationName);  131             log.setMethod((joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()")+"."+operationType);  132             log.setLogType((long)0);  133             log.setRequestIp(ip);  134             log.setExceptioncode( null);  135             log.setExceptionDetail( null);  136             log.setParams( null);  137             log.setCreateBy(user.getName());  138             log.setCreateDate(new Date());  139             //保存數(shù)據(jù)庫(kù)  140             systemLogService.insert(log);  141             System.out.println("=====controller后置通知結(jié)束=====");  142         }  catch (Exception e) {  143             //記錄本地異常日志  144             logger.error("==后置通知異常==");  145             logger.error("異常信息:{}", e.getMessage());  146         }  147     } 148     149     //配置后置返回通知,使用在方法aspect()上注冊(cè)的切入點(diǎn)150       @AfterReturning("controllerAspect()")151       public void afterReturn(JoinPoint joinPoint){152           System.out.println("=====執(zhí)行controller后置返回通知=====");  153               if(logger.isInfoEnabled()){154                   logger.info("afterReturn " + joinPoint);155               }156       }157     158     /** 159      * 異常通知 用于攔截記錄異常日志 160      * 161      * @param joinPoint 162      * @param e 163      */  164      @AfterThrowing(pointcut = "controllerAspect()", throwing="e")  165      public  void doAfterThrowing(JoinPoint joinPoint, Throwable e) {  166         /*HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();  167         HttpSession session = request.getSession();  168         //讀取session中的用戶  169         User user = (User) session.getAttribute(WebConstants.CURRENT_USER);  170         //獲取請(qǐng)求ip  171         String ip = request.getRemoteAddr(); */ 172         //獲取用戶請(qǐng)求方法的參數(shù)并序列化為JSON格式字符串  173         174         User user = new User();175         user.setId(1);176         user.setName("張三");177         String ip = "127.0.0.1";178         179         String params = "";  180          if (joinPoint.getArgs() !=  null && joinPoint.getArgs().length > 0) {  181              for ( int i = 0; i < joinPoint.getArgs().length; i++) {  182                 params += JsonUtil.getJsonStr(joinPoint.getArgs()[i]) + ";";  183             }  184         }  185          try {  186              187              String targetName = joinPoint.getTarget().getClass().getName();  188              String methodName = joinPoint.getSignature().getName();  189              Object[] arguments = joinPoint.getArgs();  190              Class targetClass = Class.forName(targetName);  191              Method[] methods = targetClass.getMethods();192              String operationType = "";193              String operationName = "";194               for (Method method : methods) {  195                   if (method.getName().equals(methodName)) {  196                      Class[] clazzs = method.getParameterTypes();  197                       if (clazzs.length == arguments.length) {  198                           operationType = method.getAnnotation(Log.class).operationType();199                           operationName = method.getAnnotation(Log.class).operationName();200                           break;  201                      }  202                  }  203              }204              /*========控制臺(tái)輸出=========*/  205             System.out.println("=====異常通知開(kāi)始=====");  206             System.out.println("異常代碼:" + e.getClass().getName());  207             System.out.println("異常信息:" + e.getMessage());  208             System.out.println("異常方法:" + (joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()")+"."+operationType);  209             System.out.println("方法描述:" + operationName);  210             System.out.println("請(qǐng)求人:" + user.getName());  211             System.out.println("請(qǐng)求IP:" + ip);  212             System.out.println("請(qǐng)求參數(shù):" + params);  213                /*==========數(shù)據(jù)庫(kù)日志=========*/  214             SystemLog log = new SystemLog();215             log.setId(UUID.randomUUID().toString());216             log.setDescription(operationName);  217             log.setExceptioncode(e.getClass().getName());  218             log.setLogType((long)1);  219             log.setExceptionDetail(e.getMessage());  220             log.setMethod((joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()"));  221             log.setParams(params);  222             log.setCreateBy(user.getName());  223             log.setCreateDate(new Date());  224             log.setRequestIp(ip);  225             //保存數(shù)據(jù)庫(kù)  226             systemLogService.insert(log);  227             System.out.println("=====異常通知結(jié)束=====");  228         }  catch (Exception ex) {  229             //記錄本地異常日志  230             logger.error("==異常通知異常==");  231             logger.error("異常信息:{}", ex.getMessage());  232         }  233          /*==========記錄本地異常日志==========*/  234         logger.error("異常方法:{}異常代碼:{}異常信息:{}參數(shù):{}", joinPoint.getTarget().getClass().getName() + joinPoint.getSignature().getName(), e.getClass().getName(), e.getMessage(), params);  235   236     }  237     238 }

 我這里寫(xiě)的比較全,前置通知,環(huán)繞通知,后置通知,異常通知,后置飯后通知,都寫(xiě)上了,在我們實(shí)際編寫(xiě)中不寫(xiě)全也沒(méi)事,我習(xí)慣上把記錄日志的邏輯寫(xiě)在后置通知里面,我看網(wǎng)上也有些在前置通知里面的,但我感覺(jué)寫(xiě)在后置通知里比較好。

下面開(kāi)始在controller中加入自定義的注解!!

 1 package com.gcx.controller; 2  3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.stereotype.Controller; 5 import org.springframework.web.bind.annotation.RequestMapping; 6  7 import com.gcx.annotation.Log; 8 import com.gcx.service.UserService; 9 10 @Controller11 @RequestMapping("userController")12 public class UserController {13 14     @Autowired15     private UserService userService;16     17     @RequestMapping("testAOP")18     @Log(operationType="add操作:",operationName="添加用戶")  19     public void testAOP(String userName,String passWord){        20         userService.addUser(userName, password);21     }22 }

下面編寫(xiě)測(cè)試類

1 @Test2     public void testAOP1(){3         //啟動(dòng)Spring容器        4         ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"classpath:applicationContext-mvc.xml","classpath:applicationContext-dataSource.xml"});5         //獲取service或controller組件6         UserController userController = (UserController) ctx.getBean("userController");7         userController.testAOP("zhangsan", "123456");8     }9     

數(shù)據(jù)庫(kù)數(shù)據(jù):

我原本想寫(xiě)兩個(gè)切點(diǎn),一個(gè)是service層,一個(gè)是controller層,service層是用來(lái)記錄異常信息的日志,而controller層的是用來(lái)記錄功能的日志,運(yùn)行結(jié)果如下。    

這樣做的話不知道在實(shí)際的項(xiàng)目中運(yùn)行效率好不好,在這里請(qǐng)看到博客的大牛給點(diǎn)建議!!


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 逊克县| 比如县| 河北省| 麻栗坡县| 乌兰浩特市| 宣汉县| 紫云| 车致| 剑河县| 高清| 聊城市| 中方县| 广安市| 濮阳县| 永修县| 鄂托克前旗| 自贡市| 赫章县| 贵德县| 建平县| 乳山市| 忻城县| 天峻县| 莒南县| 明星| 鄂托克前旗| 建德市| 沙雅县| 北安市| 赫章县| 海兴县| 全州县| 慈利县| 宁城县| 玉田县| 河北省| 诸暨市| 郁南县| 三门峡市| 虹口区| 溧阳市|