I'm writing custom aspect handles annotation for method invocation time measurement. I'm not sure that no one will implement his custom aspect for the same annotation. But I want to be sure that my aspect will be the last in annotation handling order for the most accurate time measurement.
If I put two aspects in context with @Order
specification by my own
@Component
@Order(HIGHEST_PRECEDENCE)
@Aspect
public class Aspect1{}
@Component
@Order(LOWEST_PRECEDENCE)
@Aspect
public class Aspect2{}
it works well.
But if I specify @Order only above Aspect2
, the order breaks. I can't be sure that any who wan't to handle the same annotation will implement Ordered
interface or specify @Order
annotation.
There're certain rules about AOP handling in spring. If you don't implement the Ordered
interface or don't have Order
annotation imply you don't care about the order and such aspects are put at the end of ordered aspects.
For example
Class1 has implemented Ordered
with order 10
Class2 has Order
annotation with value 2
Class3 and Class4 neither have Order
annotation nor they implement Ordered
interfaces. In this case, Class3 and Class 4 can be in any order.
For this example, if all Aspects satisfy the execution criteria then they will be called as Class1 <- Class2 <- Class3 <- Class4
or Class1 <- Class2 <- Class4 <- Class3
So if you really want your annotation should be at the last, then you should not implement the Ordered
interface and should not have Order
annotation. But as said this can be problematic as well if other Aspect also satisfies the same execution criteria and they also have neither Order
annotation nor that have implemented Ordered
annotation.