在 Spring boot里配置AOP其实很简单,只需在pom文件里加入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
然后配置切面类,下面是配置了一个环绕通知
@Aspect
@Component
public class LoggerInterceptor {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Around("within(com..*) && @annotation(log)")
public Object doAroundMethod(ProceedingJoinPoint pjd, Log log) throws Throwable {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
//路径
logger.info("方法路径:{}",pjd.getSignature().getDeclaringTypeName()+'.'+ pjd.getSignature().getName());//获取类名及类方法
//ip
logger.info(&