前提條件:
除了spring相關jar包外,還需要引入aspectj包。
- <dependency>
- <groupId>org.aspectj</groupId>
- <artifactId>aspectjweaver</artifactId>
- <version>1.7.2</version>
- </dependency>
要實現此功能,必須完成以下幾步:
1.在springmvc-servlet.xml中實現對AOP的支持
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-4.0.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
- <aop:aspectj-autoproxy proxy-target-class="true"/>
- <bean class="com.yusj.interceptor.LogAspect" />
- .
- .
- .
- .
- </beans>
2.注解的方法實現Aspect
- package com.yusj.core.interceptor;
- import java.text.SimpleDateFormat;
- import java.util.HashMap;
- import java.util.Map;
- import javax.servlet.http.HttpServletRequest;
- import org.aspectj.lang.JoinPoint;
- import org.aspectj.lang.ProceedingJoinPoint;
- import org.aspectj.lang.annotation.After;
- import org.aspectj.lang.annotation.Around;
- import org.aspectj.lang.annotation.Aspect;
- import org.aspectj.lang.annotation.Before;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.web.context.request.RequestAttributes;
- import org.springframework.web.context.request.RequestContextHolder;
- import org.springframework.web.context.request.ServletRequestAttributes;
- import com.google.gson.Gson;
- /**
- *
- * @ClassName: LogAspect
- * @Description: 日志記錄AOP實現
- * @author shaojian.yu
- * @date 2014年11月3日 下午1:51:59
- *
- */
- @Aspect
- public class LogAspect {
- private final Logger logger = LoggerFactory.getLogger(this.getClass());
- private String requestPath = null ; // 請求地址
- private String userName = null ; // 用戶名
- private Map<?,?> inputParamMap = null ; // 傳入參數
- private Map<String, Object> outputParamMap = null; // 存放輸出結果
- private long startTimeMillis = 0; // 開始時間
- private long endTimeMillis = 0; // 結束時間
- /**
- *
- * @Title:doBeforeInServiceLayer
- * @Description: 方法調用前觸發
- * 記錄開始時間
- * @author shaojian.yu
- * @date 2014年11月2日 下午4:45:53
- * @param joinPoint
- */
- @Before("execution(* com.yusj.controller..*.*(..))")
- public void doBeforeInServiceLayer(JoinPoint joinPoint) {
- startTimeMillis = System.currentTimeMillis(); // 記錄方法開始執行的時間
- }
- /**
- *
- * @Title:doAfterInServiceLayer
- * @Description: 方法調用后觸發
- * 記錄結束時間
- * @author shaojian.yu
- * @date 2014年11月2日 下午4:46:21
- * @param joinPoint
- */
- @After("execution(* com.yusj.controller..*.*(..))")
- public void doAfterInServiceLayer(JoinPoint joinPoint) {
- endTimeMillis = System.currentTimeMillis(); // 記錄方法執行完成的時間
- this.printOptLog();
- }
- /**
- *
- * @Title:doAround
- * @Description: 環繞觸發
- * @author shaojian.yu
- * @date 2014年11月3日 下午1:58:45
- * @param pjp
- * @return
- * @throws Throwable
- */
- @Around("execution(* com.yusj.controller..*.*(..))")
- public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
- /**
- * 1.獲取request信息
- * 2.根據request獲取session
- * 3.從session中取出登錄用戶信息
- */
- RequestAttributes ra = RequestContextHolder.getRequestAttributes();
- ServletRequestAttributes sra = (ServletRequestAttributes)ra;
- HttpServletRequest request = sra.getRequest();
- // 從session中獲取用戶信息
- String loginInfo = (String) session.getAttribute("username");
- if(loginInfo != null && !"".equals(loginInfo)){
- userName = operLoginModel.getLogin_Name();
- }else{
- userName = "用戶未登錄" ;
- }
- // 獲取輸入參數
- inputParamMap = request.getParameterMap();
- // 獲取請求地址
- requestPath = request.getRequestURI();
- // 執行完方法的返回值:調用proceed()方法,就會觸發切入點方法執行
- outputParamMap = new HashMap<String, Object>();
- Object result = pjp.proceed();// result的值就是被攔截方法的返回值
- outputParamMap.put("result", result);
- return result;
- }
- /**
- *
- * @Title:printOptLog
- * @Description: 輸出日志
- * @author shaojian.yu
- * @date 2014年11月2日 下午4:47:09
- */
- private void printOptLog() {
- Gson gson = new Gson(); // 需要用到google的gson解析包
- String optTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(startTimeMillis);
- logger.info("\n user:"+userName
- +" url:"+requestPath+"; op_time:" + optTime + " pro_time:" + (endTimeMillis - startTimeMillis) + "ms ;"
- +" param:"+gson.toJson(inputParamMap)+";"+"\n result:"+gson.toJson(outputParamMap));
- }
- }