Reputation: 1777
What would be the order of invocations on the "way in" if I have 3 aspects like:
@Order(Ordered.HIGHEST_PRECEDENCE)
public class Aspect1
public class Aspect2
@Order(Ordered.LOWEST_PRECEDENCE)
public class Aspect3
so Aspect2 doesn't have any order annotation.
It is clear that Aspect1 will be invoked before Aspect3, but does it mean that Aspect2 will be always invoked in between ?
Upvotes: 1
Views: 2869
Reputation: 2528
From the official javadoc about @Order:
Annotation that defines ordering. The value is optional, and represents order value as defined in the Ordered interface. Lower values have higher priority. The default value is Ordered.LOWEST_PRECEDENCE, indicating lowest priority (losing to any other specified order value).
This means that Aspect2
will also have a value of Ordered.LOWEST_PRECEDENCE
, which is Integer.MAX_VALUE
. I don't think there is a way of being 100% sure that it will always be invoked before Aspect3
- I believe it depends on the order of component scan.
If you want to be sure, you can put in exact values:
@Order(1)
public class Aspect1 {...}
@Order(2)
public class Aspect2 {...}
@Order(3)
public class Aspect3 {...}
Or just put a value between Integer.MIN_VALUE
and Integer.MAX_VALUE
on Aspect2
:
@Order(0)
public class Aspect2 {...}
Here is also a test which confirms this:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes =
{Class1.class, Class3.class, Class2.class})
public class TestTest {
public static interface Test {}
@Order(Ordered.HIGHEST_PRECEDENCE)
@Component
public static class Class1 implements Test {}
@Order
@Component
public static class Class2 implements Test {}
@Order(Ordered.LOWEST_PRECEDENCE)
@Component
public static class Class3 implements Test {}
@Autowired
List<Test> list;
@Test
public void test() {
System.out.println("list: " + list);
}
}
This will output:
list: [TestTest$Class1@d21a74c, TestTest$Class3@6e509ffa, TestTest$Class2@2898ac89]
But if you change the order of classes 2
and 3
in @SpringApplicationConfiguration
, Class2
will be invoked second.
Upvotes: 2