我在所有RestController注释控制器中都有一个带切入点的Aspect:
@Aspect
@Component
public class ControllerLoggingAspect {
Logger log = LoggerFactory.getLogger(this.getClass());
@Pointcut("within(@org.springframework.web.bind.annotation.RestController *)")
public void controller() {
}
@Before("controller()")
public void logBefore(JoinPoint joinPoint) {
log.info("START REST CONTROLLER");
}
@After("controller()")
public void logAfter(JoinPoint joinPoint) {
log.info("END REST CONTROLLER");
}
}
pom依赖项:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.12</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.7.3</version>
</dependency>
我在日志中收到以下错误:
NoSuchMethodError: org.springframework.core.annotation.OrderUtils.getOrder
我可以在项目中验证我的OrderUtils
类是否有getOrder
方法。
如果我删除@Before
和@After
带注释的方法,Spring会启动时没有错误。
有没有人知道我错过了什么?
答案 0 :(得分:0)
原来我的spring-aop
和spring-core
依赖项之间的版本不同。修复它们以使AOP按预期工作。
答案 1 :(得分:0)
使AOP类成为after注释:
@Aspect
@Configuration
public class AfterAopAspect {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@AfterReturning(value = "execution(* com.alok.*.*(..))", returning = "result")
public void afterReturning(JoinPoint joinPoint, Object result) {
logger.info("{} returned with value {}", joinPoint, result);
}
@After(value = "execution(* com.alok.*.*(..))")
public void after(JoinPoint joinPoint) {
logger.info("after execution of {}", joinPoint);
}
}