Aop实现见代码,简单demo实现
1 package com.idcos.automate.config; 2 3 import com.idcos.automate.dal.auto.dao.dcos.DcosLogDAO; 4 import com.idcos.automate.dal.auto.dataobject.dcos.DcosLogDO; 5 import org.aspectj.lang.JoinPoint; 6 import org.aspectj.lang.ProceedingJoinPoint; 7 import org.aspectj.lang.annotation.AfterThrowing; 8 import org.aspectj.lang.annotation.Around; 9 import org.aspectj.lang.annotation.Aspect; 10 import org.aspectj.lang.annotation.Pointcut; 11 import org.slf4j.LoggerFactory; 12 import org.springframework.beans.factory.annotation.Autowired; 13 import org.springframework.stereotype.Component; 14 import org.springframework.web.context.request.RequestContextHolder; 15 import org.springframework.web.context.request.ServletRequestAttributes; 16 17 import javax.servlet.http.HttpServletRequest; 18 import java.math.BigDecimal; 19 import java.util.Arrays; 20 21 /** 22 * @author GuanBin 23 * @version LoggerConfig.java, v1 2018/4/24 下午5:34 GuanBin Exp $$ 24 */ 25 @Aspect//切面注解 26 @Component//@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。 27 public class LoggerConfig { 28 29 @Autowired 30 private DcosLogDAO dcosLogDAO; 31 32 private final org.slf4j.Logger log = LoggerFactory.getLogger(this.getClass()); 33 34 35 //定义切点,监听所有的web下的controller 36 @Pointcut("execution(public * com.idcos.automate.web..*.*(..))") 37 public void webLog() { 38 } 39 40 //抛出切点连接中的异常信息 41 @AfterThrowing(pointcut = "webLog()", throwing = "e") 42 public void logAfterThrowing(JoinPoint joinPoint, Throwable e) { 43 log.error("Exception in {}.{}.() cause = {}", joinPoint.getSignature().getDeclaringType(), joinPoint.getSignature().getName(), e.getCause() != null ? e.getCause() : null); 44 } 45 46 /** 47 * 在方法前和方法后执行 48 */ 49 @Around("webLog()") 50 public void logWebAccessInfo(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { 51 52 long start = System.currentTimeMillis(); 53 //JoinPoint、ProceedingJoinPoint提供访问当前被通知方法的目标对象、代理对象、方法参数等数据,其中ProceedingJoinPoint提供访问当前被通知方法的目标对象 54 // 在环绕Around时使用: 55 proceedingJoinPoint.proceed(); 56 long end = System.currentTimeMillis();//测试方法执行后的时间是多少 57 float time = BigDecimal.valueOf(end-start).divide(BigDecimal.valueOf(1000)).setScale(3, BigDecimal.ROUND_HALF_UP).floatValue(); 58 59 ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); 60 HttpServletRequest request = attributes.getRequest(); 61 62 // 记录下请求内容 63 log.info("URL : " + request.getRequestURL().toString());//请求的url 64 log.info("HTTP_METHOD : " + request.getMethod());//请求的方法 65 log.info("IP : " + request.getRemoteAddr());//请求方的ip 66 log.info("CLASS_METHOD : " + proceedingJoinPoint.getSignature().getDeclaringTypeName() + "." + proceedingJoinPoint.getSignature().getName()); 67 log.info("ARGS : " + Arrays.toString(proceedingJoinPoint.getArgs()));//请求的参数 68 69 DcosLogDO dcosLogDO = new DcosLogDO(); 70 71 dcosLogDO.setClassName(request.getClass().getName()); 72 dcosLogDO.setMethodName(request.getMethod()); 73 dcosLogDO.setUrl(request.getRemoteAddr()+request.getRequestURL()); 74 dcosLogDO.setTime(Float.toString(time)); 75 dcosLogDAO.saveAndFlush(dcosLogDO); 76 } 77 78 79 }
可参考:
https://blog.csdn.net/zhengchao1991/article/details/53391244