• @AutoConfigureBefore、@AutoConfigureAfter使用细节


    背景

    我们在定义spring-boot-starter时,有时会使用@AutoConfigureBefore@AutoConfigureAfter来指定自动装配配置之间的加载顺序。

    使用此注解要注意两点:

    • 被注解修饰的类要在spring.factories中的org.springframework.boot.autoconfigure.EnableAutoConfiguration指定,不能用@Configuration修饰,否则会立刻被spring扫描到,不能实现指定顺序,之前的文章介绍过
    • @AutoConfigureBefore@AutoConfigureAfter的作用是指定配置类加载的顺序,而不是bean的加载顺序,这点要注意,很多人也是疏忽了这点,本文重点分析的就是此部分

    案例

    spring.factories
    org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
      test.TestConfig2,\
      test.TestConfig
    
    • 1
    • 2
    • 3
    TestConfig
    public class TestConfig {
        
        public TestConfig(){
            System.out.println("====TestConfig====");
        }
        
        @Bean
        public Base base(){
            System.out.println("====base1====");
            return new Base(1);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    TestConfig2
    @AutoConfigureBefore(TestConfig.class)
    public class TestConfig2 {
        
        public TestConfig2(){
            System.out.println("====TestConfig2====");
        }
        
        @Bean
        public Base base(){
            System.out.println("====base2====");
            return new Base(2);
        }
        
        @Bean
        public Test test(Base base){
            return new Test(base);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    Test
    public class Test {
        private Base base;
        
        public Test(Base base){
            this.base = base;
        }
    
        @PostConstruct
        public void init(){
            System.out.println("====" + base.i + "====");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • TestConfigTestConfig2使用自动装配配置
    • TestConfigTestConfig2都生成了类型Base,名字base的bean对象
    • TestConfig2使用@AutoConfigureBefore指定在TestConfig之前进行加载
    • TestConfig2生成了类型Test,名字test的bean对象并注入了类型Bean的bean对象
    结果
    ====TestConfig2====
    ====TestConfig====
    ====base1====
    ====1====
    
    • 1
    • 2
    • 3
    • 4
    结论
    • @AutoConfigureBefore可以指定TestConfig2TestConfig之前进行生成加载
    • 但不能指定@Bean生成对象的顺序,这里看到注入的Base类型对象是TestConfig生成的

    那么要指定bean的加载顺序怎么做?

    可以用@ConditionalOnMissingBean注解

    案例中只修改TestConfig,其余不变

    TestConfig
    public class TestConfig {
        
        public TestConfig(){
            System.out.println("====TestConfig====");
        }
        
        @Bean
        @ConditionalOnMissingBean
        public Base base(){
            System.out.println("====base1====");
            return new Base(1);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    结果
    ====TestConfig2====
    ====base2====
    ====2====
    ====TestConfig====
    
    • 1
    • 2
    • 3
    • 4

    可以看到可以通过@ConditionalOnMissingBean来控制bean对象的加载顺序

  • 相关阅读:
    [java入门到精通] 11 泛型,数据结构,List,Set
    pyppeteer框架基本语法备忘录
    Spring Security学习笔记(一)Spring Security架构原理
    基于线性核函数的SVM数据分类算法matlab仿真
    Python数据分析--Numpy常用函数介绍(3)
    聚焦数字化项目管理——2023年PMI项目管理大会亮点回顾
    竞赛选题 基于深度学习的目标检测算法
    java System
    Apache Doris系列之:深入认识实时分析型数据库Apache Doris
    Android开发学习日记--页面间传递数据
  • 原文地址:https://blog.csdn.net/guntun8987/article/details/132720847