<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
*/ @Aspect @Component @Slf4j public class HttpAspect { /** * 日志切点 */ @Pointcut("execution(public * com.api.controller..*.*(..))") public void log() { } /** * 开始请求前 * * @param joinPoint */ @Before("log()") public void doBefore(JoinPoint joinPoint) { // 主类 ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); if (attributes != null){ HttpServletRequest request = attributes.getRequest(); // url 路径 log.info("请求={}", request.getRequestURL() + " | method=" + request.getMethod() + " | ip=" + request.getRemoteAddr()); // 类方法 log.info("方法={}", joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName()); // 参数 log.info("参数={}", Arrays.toString(joinPoint.getArgs())); } } /** * 返回处理 * * @param result */ @AfterReturning(returning = "result", pointcut = "log()") public void doAfterReturning(Object result) { log.info("返回={}", JSON.toJSON(result)); }
execution(public * *(..))
execution(* set*(..))
execution(* cn.freemethod.business.pack.Say.*(..))
execution(* cn.freemethod.business.*.*(..))
execution(* cn.freemethod.business..*.*(..))
within(com.xyz.service.*)
within(com.xyz.service..*)
this(com.xyz.service.AccountService)
target(com.xyz.service.AccountService)
一般情况下代理类(Proxy)和目标类(Target)都实现了相同的接口,所以上面的2个基本是等效的。
args(java.io.Serializable)
只要这个参数实现了java.io.Serializable接口就可以,不管是java.io.Serializable还是Integer,还是String都可以。
@target(org.springframework.transaction.annotation.Transactional)
@within(org.springframework.transaction.annotation.Transactional)
@annotation(org.springframework.transaction.annotation.Transactional)
@args(org.springframework.transaction.annotation.Transactional)
注意是参数类型上有Transactional注解,而不是方法的参数上有注解。
bean(simpleSay)
bean名字为simpleSay中的所有方法。
bean(*Impl)
bean名字匹配*Impl的bean中的所有方法。