spring annotation advice order

瘦欲@ 提交于 2020-01-23 06:05:10

问题


I have a method with two annotations

@One
@Two
public Object foo() { ... }

I have two aspects that use these annotations

@Around("@annotation(One)")
public Object doOne(final ProceedingJoinPoint joinPoint) throws Throwable { ... }

and

@Around("@annotation(Two)")
public Object doTwo(final ProceedingJoinPoint joinPoint) throws Throwable { ... }

But is the order in which these advices are executed indeterminate?


回答1:


The order is undefined. If you need determinate order, use @Order annotation.

See also:

  • 7.2.4.7 Advice ordering



回答2:


6.2.4.7. Advice ordering

What happens when multiple pieces of advice all want to run at the same join point? Spring AOP follows the same precedence rules as AspectJ to determine the order of advice execution. The highest precedence advice runs first "on the way in" (so given two pieces of before advice, the one with highest precedence runs first). "On the way out" from a join point, the highest precedence advice runs last (so given two pieces of after advice, the one with the highest precedence will run second).

When two pieces of advice defined in different aspects both need to run at the same join point, unless you specify otherwise the order of execution is undefined. You can control the order of execution by specifying precedence. 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. Given two aspects, the aspect returning the lower value from Ordered.getValue() (or the annotation value) has the higher precedence.

When two pieces of advice defined in the same aspect both need to run at the same join point, the ordering is undefined (since there is no way to retrieve the declaration order via reflection for javac-compiled classes). Consider collapsing such advice methods into one advice method per joinpoint in each aspect class, or refactor the pieces of advice into separate aspect classes - which can be ordered at the aspect level.

http://static.springsource.org/spring/docs/2.0.x/reference/aop.html




回答3:


Order is undefined unless explicitly indicated (for instance, by using @Order)




回答4:


  1. On the way in to a joinpoint, the advice with lowest Order value gets executed first.

  2. On the way out from the joinpoint, the advice with highest Order value gets executed first.

  3. When two pieces of advice defined in the same aspect both need to run at the same join point, the ordering is undefined.Consider collapsing such advice methods into one advice method per join point in each aspect class, or refactor the pieces of advice into separate aspect classes - which can be ordered at the aspect level.


来源:https://stackoverflow.com/questions/8818143/spring-annotation-advice-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!