Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring annotation advice order

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?

like image 360
Paul McKenzie Avatar asked Jan 11 '12 11:01

Paul McKenzie


People also ask

Does order of Java annotations matter?

More than one annotation clause may apply to an entity. The order in which these annotations are given does not matter. The meaning of annotation clauses is implementation-dependent. On the Java platform, the following annotations have a standard meaning.

Which list of five advice types is correct in Spring AOP?

There are five types of advices in the Spring AOP framework: before, after, after-returning, after-throwing, and around advice. Advices are taken for a particular join point. We will discuss these advices further in this section. Target object: An object on which advices are applied, is called the target object.

What is order annotation in Spring?

The @Order annotation defines the sorting order of an annotated component or bean. It has an optional value argument which determines the order of the component; the default value is Ordered. LOWEST_PRECEDENCE. This marks that the component has the lowest priority among all other ordered components.

What is the order of bean creation in Spring?

The order in which Spring container loads beans cannot be predicted. There's no specific ordering logic specification given by Spring framework. But Spring guarantees if a bean A has dependency of B (e.g. bean A has an instance variable @Autowired B b; ) then B will be initialized first.


2 Answers

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

See also:

  • 7.2.4.7 Advice ordering
like image 180
axtavt Avatar answered Nov 12 '22 19:11

axtavt


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

like image 35
JuanZe Avatar answered Nov 12 '22 17:11

JuanZe