转自:https://blog.csdn.net/l718161567/article/details/74912424
利用@Order控制配置类/AOP/方法/字段的加载顺序
1.AOP加载顺序
@Component
@Aspect
@Order(1)
public class Aspect1 {
...
System.out.println("aop-1 加载了");
}
@Component
@Aspect
@Order(2)
public class Aspect2 {
...
System.out.println("aop-2 加载了");
}
执行顺序:
aop-1 加载了
aop-2 加载了
2.配置类加载顺序
@Configuration
@Order(1)
public class DemoConfig1 {
@Bean
public Demo1Service demo1Service(){
System.out.println("config-1 加载了");
return new Demo1Service();
}
}
@Configuration
@Order(2)
public class DemoConfig2 {
@Bean
public Demo2Service demo2Service(){
System.out.println("config-2 加载了");
return new Demo2Service();
}
}
执行顺序:
config-1 加载了
config-2 加载了
tips:
Integer.MIN_VALUE <= Order <= Integer.MAX_VALUE
Order值越小,优先级越高!
@Order的作用范围:
ElementType.TYPE, ElementType.METHOD, ElementType.FIELD