如何更改由相同注释(例如 AspectJ 中的@Around)注释的两个或多个建议的执行顺序?
How can I change the executing order of two or more advice annotated by the same annotation such as @Around in AspectJ?
这是我的代码:
@Pointcut("execution(* *(..))")
public void cutPointToken() {}
@Pointcut("execution(* *(..))")
public void cutPointEmptyParam() {}
@Around("cutPointToken()")
public Object authenticateToken(ProceedingJoinPoint joinPoint) throws Throwable {
LOGGER.info("authenticate -- start --");
...
Object o = joinPoint.proceed();
LOGGER.info("authenticate -- end --");
return o;
}
@Around("cutPointEmptyParam()")
public Object checkParameter(ProceedingJoinPoint joinPoint) throws Throwable {
LOGGER.info("check param -- start --");
...
Object o = joinPoint.proceed();
LOGGER.info("check param -- end --");
return o;
}
我得到了:
authenticate -- start --
check param -- start --
...
check param -- end --
authenticate -- end --
预计:
check param -- start --
authenticate -- start --
...
authenticate -- end --
check param -- end --
如何更改这两个方法的执行顺序?
尝试了 @Order
注释,@Order(1)
在 checkParameter
方法上,@Order(2)
在另一个方法上,但它不起作用。
使用 @Order
注释的想法是正确的,但是,将其置于 class 级别,如文档 7.2.4.7 Advice ordering 所述。
This is done in the normal Spring way by either implementing the org.springframework.core. Ordered interface in the aspect class or annotating it with the Order annotation.
用 @Aspect
注释的方法上的放置将不起作用,因为它没有注册为 bean。在 1.9.2. @Autowired 部分找到 @Order
注释。
The @Order
annotation may be declared at target class level but also on @Bean
methods...
@Aspect
@Order(1)
public class CheckParameterAspect {
@Around("cutPointEmptyParam()")
public Object checkParameter(ProceedingJoinPoint joinPoint) throws Throwable {
//...
}
}
@Aspect
@Order(2)
public class AuthenticateTokenAspect {
@Around("cutPointToken()")
public Object authenticateToken(ProceedingJoinPoint joinPoint) throws Throwable {
//...
}
}
编辑:0
的排序似乎是可能的。
这是我的代码:
@Pointcut("execution(* *(..))")
public void cutPointToken() {}
@Pointcut("execution(* *(..))")
public void cutPointEmptyParam() {}
@Around("cutPointToken()")
public Object authenticateToken(ProceedingJoinPoint joinPoint) throws Throwable {
LOGGER.info("authenticate -- start --");
...
Object o = joinPoint.proceed();
LOGGER.info("authenticate -- end --");
return o;
}
@Around("cutPointEmptyParam()")
public Object checkParameter(ProceedingJoinPoint joinPoint) throws Throwable {
LOGGER.info("check param -- start --");
...
Object o = joinPoint.proceed();
LOGGER.info("check param -- end --");
return o;
}
我得到了:
authenticate -- start --
check param -- start --
...
check param -- end --
authenticate -- end --
预计:
check param -- start --
authenticate -- start --
...
authenticate -- end --
check param -- end --
如何更改这两个方法的执行顺序?
尝试了 @Order
注释,@Order(1)
在 checkParameter
方法上,@Order(2)
在另一个方法上,但它不起作用。
使用 @Order
注释的想法是正确的,但是,将其置于 class 级别,如文档 7.2.4.7 Advice ordering 所述。
This is done in the normal Spring way by either implementing the org.springframework.core. Ordered interface in the aspect class or annotating it with the Order annotation.
用 @Aspect
注释的方法上的放置将不起作用,因为它没有注册为 bean。在 1.9.2. @Autowired 部分找到 @Order
注释。
The
@Order
annotation may be declared at target class level but also on@Bean
methods...
@Aspect
@Order(1)
public class CheckParameterAspect {
@Around("cutPointEmptyParam()")
public Object checkParameter(ProceedingJoinPoint joinPoint) throws Throwable {
//...
}
}
@Aspect
@Order(2)
public class AuthenticateTokenAspect {
@Around("cutPointToken()")
public Object authenticateToken(ProceedingJoinPoint joinPoint) throws Throwable {
//...
}
}
编辑:0
的排序似乎是可能的。