利用@Order控制配置类/AOP/方法/字段的加载顺序

来源:互联网 发布:js中鼠标移动事件 编辑:程序博客网 时间:2024/06/04 08:46

1.AOP加载顺序

    @Component    @Aspect    @Order(1)    public class Ascpect {        ...        System.out.println("aop-1 加载了");    }
    @Component    @Aspect    @Order(2)    public class Ascpect {        ...        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
原创粉丝点击