Spring-boot 配置Aop获取controller里的request中的参数以及其返回值

转自:http://ysj5125094.iteye.com/blog/2151855

前提条件:

除了spring相关jar包外,还需要引入aspectj包。

Xml代码 

  1. <dependency>  
  2.         <groupId>org.aspectj</groupId>  
  3.         <artifactId>aspectjweaver</artifactId>  
  4.         <version>1.7.2</version>  
  5.     </dependency>  

要实现此功能,必须完成以下几步:

1.在springmvc-servlet.xml中实现对AOP的支持

Xml代码 

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"   
  3.     xmlns:aop="http://www.springframework.org/schema/aop"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  5.     xmlns:context="http://www.springframework.org/schema/context"  
  6.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  7.     xsi:schemaLocation="     
  8.            http://www.springframework.org/schema/beans     
  9.            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd     
  10.            http://www.springframework.org/schema/context     
  11.            http://www.springframework.org/schema/context/spring-context-4.0.xsd    
  12.            http://www.springframework.org/schema/mvc     
  13.            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd  
  14.            http://www.springframework.org/schema/aop   
  15.            http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">       
  16.     <aop:aspectj-autoproxy proxy-target-class="true"/>  
  17.     <bean class="com.yusj.interceptor.LogAspect" />  
  18. </beans>  

2.注解的方法实现Aspect

Java代码 

  1. package com.yusj.core.interceptor;  
  2. import java.text.SimpleDateFormat;  
  3. import java.util.HashMap;  
  4. import java.util.Map;  
  5. import javax.servlet.http.HttpServletRequest;  
  6. import org.aspectj.lang.JoinPoint;  
  7. import org.aspectj.lang.ProceedingJoinPoint;  
  8. import org.aspectj.lang.annotation.After;  
  9. import org.aspectj.lang.annotation.Around;  
  10. import org.aspectj.lang.annotation.Aspect;  
  11. import org.aspectj.lang.annotation.Before;  
  12. import org.slf4j.Logger;  
  13. import org.slf4j.LoggerFactory;  
  14. import org.springframework.web.context.request.RequestAttributes;  
  15. import org.springframework.web.context.request.RequestContextHolder;  
  16. import org.springframework.web.context.request.ServletRequestAttributes;  
  17. import com.google.gson.Gson;  
  18. /** 
  19.  *  
  20. * @ClassName: LogAspect  
  21. * @Description: 日志记录AOP实现  
  22. * @author shaojian.yu 
  23. * @date 2014年11月3日 下午1:51:59  
  24.  */  
  25. @Aspect  
  26. public class LogAspect {  
  27.     private final Logger logger = LoggerFactory.getLogger(this.getClass());  
  28.   
  29.     private String requestPath = null ; // 请求地址  
  30.     private String userName = null ; // 用户名  
  31.     private Map<?,?> inputParamMap = null ; // 传入参数  
  32.     private Map<String, Object> outputParamMap = null; // 存放输出结果  
  33.     private long startTimeMillis = 0; // 开始时间  
  34.     private long endTimeMillis = 0; // 结束时间  
  35.   
  36.     /** 
  37.      *  
  38.      * @Title:doBeforeInServiceLayer 
  39.      * @Description: 方法调用前触发  
  40.      *  记录开始时间  
  41.      * @author shaojian.yu  
  42.      * @date 2014年11月2日 下午4:45:53 
  43.      * @param joinPoint 
  44.      */  
  45.     @Before("execution(* com.yusj.controller..*.*(..))")  
  46.     public void doBeforeInServiceLayer(JoinPoint joinPoint) {  
  47.         startTimeMillis = System.currentTimeMillis(); // 记录方法开始执行的时间  
  48.     }  
  49.   
  50.     /** 
  51.      *  
  52.      * @Title:doAfterInServiceLayer 
  53.      * @Description: 方法调用后触发  
  54.      *  记录结束时间 
  55.      * @author shaojian.yu  
  56.      * @date 2014年11月2日 下午4:46:21 
  57.      * @param joinPoint 
  58.      */  
  59.     @After("execution(* com.yusj.controller..*.*(..))")  
  60.     public void doAfterInServiceLayer(JoinPoint joinPoint) {  
  61.         endTimeMillis = System.currentTimeMillis(); // 记录方法执行完成的时间  
  62.         this.printOptLog();  
  63.     }  
  64.   
  65.     /** 
  66.      *  
  67.      * @Title:doAround 
  68.      * @Description: 环绕触发  
  69.      * @author shaojian.yu  
  70.      * @date 2014年11月3日 下午1:58:45 
  71.      * @param pjp 
  72.      * @return 
  73.      * @throws Throwable 
  74.      */  
  75.     @Around("execution(* com.yusj.controller..*.*(..))")  
  76.     public Object doAround(ProceedingJoinPoint pjp) throws Throwable {  
  77.         /** 
  78.          * 1.获取request信息 
  79.          * 2.根据request获取session 
  80.          * 3.从session中取出登录用户信息 
  81.          */  
  82.         RequestAttributes ra = RequestContextHolder.getRequestAttributes();  
  83.         ServletRequestAttributes sra = (ServletRequestAttributes)ra;  
  84.         HttpServletRequest request = sra.getRequest();  
  85.         // 从session中获取用户信息  
  86.         String loginInfo = (String) session.getAttribute("username");  
  87.         if(loginInfo != null && !"".equals(loginInfo)){  
  88.             userName = operLoginModel.getLogin_Name();  
  89.         }else{  
  90.             userName = "用户未登录" ;  
  91.         }  
  92.         // 获取输入参数  
  93.         inputParamMap = request.getParameterMap();  
  94.         // 获取请求地址  
  95.         requestPath = request.getRequestURI();  
  96.           
  97.         // 执行完方法的返回值:调用proceed()方法,就会触发切入点方法执行  
  98.         outputParamMap = new HashMap<String, Object>();  
  99.         Object result = pjp.proceed();// result的值就是被拦截方法的返回值  
  100.         outputParamMap.put("result", result);  
  101.           
  102.         return result;  
  103.     }  
  104.   
  105.     /** 
  106.      *  
  107.      * @Title:printOptLog 
  108.      * @Description: 输出日志  
  109.      * @author shaojian.yu  
  110.      * @date 2014年11月2日 下午4:47:09 
  111.      */  
  112.     private void printOptLog() {  
  113.         Gson gson = new Gson(); // 需要用到google的gson解析包  
  114.         String optTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(startTimeMillis);  
  115.         logger.info("\n user:"+userName  
  116.                 +"  url:"+requestPath+"; op_time:" + optTime + " pro_time:" + (endTimeMillis - startTimeMillis) + "ms ;"  
  117.                 +" param:"+gson.toJson(inputParamMap)+";"+"\n result:"+gson.toJson(outputParamMap));  
  118.     }  
  119. }  

猜你喜欢

转载自blog.csdn.net/bbj12345678/article/details/81625959