8

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?

Paul McKenzie
  • 19,646
  • 25
  • 76
  • 120

5 Answers5

11

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

See also:

axtavt
  • 239,438
  • 41
  • 511
  • 482
  • It turns out that one of my aspects is @Transactional and so I can't set at-Order on this. – Paul McKenzie Jan 11 '12 at 11:30
  • 2
    @Paul: Order for `@Transactional` can be set by attribute of ``. – axtavt Jan 11 '12 at 11:52
  • Just wanted to add this since I was searching for how to do it in XML and couldn't find anything, anyways it seems the order you specify the around advice in the XML is the order it is executed. I've tested this a few times, if someone can confirm that'd be awesome. – Clarence Liu Oct 09 '13 at 23:16
  • Updated reference to spring 5 : https://docs.spring.io/spring-framework/docs/5.0.0.M5/spring-framework-reference/html/aop.html#aop-ataspectj-advice-ordering – Number945 Apr 04 '22 at 00:36
4

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

JuanZe
  • 8,007
  • 44
  • 58
  • Updated reference to spring 5 : https://docs.spring.io/spring-framework/docs/5.0.0.M5/spring-framework-reference/html/aop.html#aop-ataspectj-advice-ordering – Number945 Apr 04 '22 at 00:36
1
  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.
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
JavaMave
  • 51
  • 1
  • 2
    I have previously warned you about including links to your own website without disclosing that it is your website. Further, your link was previously deleted when you posted it as user "Maverick", on Nov 18. Your answer seems at least somewhat useful here, so for the second time, I will delete the link from your answer so that it is not mistaken as Spam. However, I'm not going to keep doing this. At some point, if you keep linking to your website like this, your posts are simply going to be flagged as spam and deleted. – Andrew Barber Nov 27 '12 at 20:58
1

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

pap
  • 27,064
  • 6
  • 41
  • 46
0

this is how to do:

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 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://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/aop.html#aop-ataspectj-advice-ordering

Community
  • 1
  • 1
kkkkkk
  • 632
  • 2
  • 8
  • 14