引入pom
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.7</version> <scope>runtime</scope> </dependency> <dependency> <groupId>aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.5.4</version> </dependency>
新建AOP类
package com.geebox.wmstest.config; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; import java.util.Arrays; @Aspect @Component public class WebLogAcpect { private Logger logger = LoggerFactory.getLogger(WebLogAcpect.class); /** * 定义切入点 */ @Pointcut("execution(public * com.geebox.wmstest.controller..*.*(..))") public void webLog(){} /** * 前置通知:在连接点之前执行的通知 * @param joinPoint * @throws Throwable */ @Before("webLog()") public void doBefore(JoinPoint joinPoint) throws Throwable { // 接收到请求,记录请求内容 ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); // 记录下请求内容 logger.info("URL : " + request.getRequestURL().toString()); logger.info("HTTP_METHOD : " + request.getMethod()); logger.info("IP : " + request.getRemoteAddr()); logger.info("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName()); logger.info("ARGS : " + Arrays.toString(joinPoint.getArgs())); } @AfterReturning(returning = "ret",pointcut = "webLog()") public void doAfterReturning(Object ret) throws Throwable { // 处理完请求,返回内容 logger.info("RESPONSE : " + ret); } }
来源:https://blog.csdn.net/lmb55/article/details/82470388/
常见的切面表达式
1 所有公有方法的执行
execution(public * *(..))
2 所有以set开头的公有方法的执行
execution(* set*(..))
3 AccountService接口下的所有方法的执行
execution(* com.xyz.service.AccountService.*(..))
4 com.xyz.service包下的所有方法的执行
execution(* com.xyz.service.*.*(..))
5 com.xyz.service包及其子包下的所有方法的执行
execution(* com.xyz.service..*.*(..))
6 匹配com.xyz.service包下的所有类的所有方法(不含子包)
within(com.xyz.service.*)
7 com.xyz.service包和子包的所有方法
within(com.xyz.service..*)
8 匹配AccountService的代理类(不支持通配符)
this(com.xyz.service.AccountService)
官方文档
https://docs.spring.io/spring/docs/4.2.5.RELEASE/spring-framework-reference/html/aop.html#aop-schema
aspect中获取注解
//获取方法上面的注解 MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); String dsKey = methodSignature.getMethod().getAnnotation(DS.class).value();
切入函数中获取注解
@Around("pointcut() && @annotation(ds)") public Object around(ProceedingJoinPoint joinPoint, DS ds) throws Throwable { //数据源名称 String dsKey = ds.value(); DynamicDataSourceContextHolder.setContextKey(dsKey); try { return joinPoint.proceed(); } finally { DynamicDataSourceContextHolder.removeContextKey(); } }
//Service方法上的注解:ds.value();
//Service方法上的注解:((MethodSignature) joinPoint.getSignature()).getMethod().getAnnotation(DS.class).value();
//Service类上的注解:joinPoint.getTarget().getClass().getAnnotation(DS.class).value()