说说@EnableConfigurationProperties那点事

两者的对比

  • @ConfigurationProperties

使用@ConfigurationProperties的时候,把配置类的属性与yml配置文件绑定起来的时候,还需要加上@Component注解才能绑定并注入IOC容器中,若不加上@Component,则会无效。

  • @EnableConfigurationProperties

@EnableConfigurationProperties的作用:则是将让使用了 @ConfigurationProperties 注解的配置类生效,将该类注入到 IOC 容器中,交由 IOC 容器进行管理,此时则不用再配置类上加上@Component。

实战的Demo

@ConfigurationProperties

配置类如下:

@Component
@ConfigurationProperties(prefix = "demo")
@Data
public class DemoConfig {
    private String name;
    private Integer age;
}

yml配置文件

demo:
  name: 张三
  age: 20

测试代码

@Component
public class Test implements ApplicationRunner {

    @Autowired
    DemoConfig demoConfig;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println(demoConfig);
    }
}

测试去掉配置类的@Component

错误如下:

@EnableConfigurationProperties的使用

@Component
@EnableConfigurationProperties(DemoConfig.class)
public class Test implements ApplicationRunner {

    @Autowired
    DemoConfig demoConfig;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println(demoConfig);
    }
}

效果如下:

@EnableConfigurationProperties出现的原因

主要原因

springboot中EnableAutoConfiguration的自动装配

先说结论

  • EnableAutoConfiguration自动装配的作用:即把指定的类构造成对象,并放入spring容器中,使其成为bean对象,作用类似@Bean注解。
  • springboot启动的时候,会扫描该项目下所有spring.factories文件。

举例解释

  • springboot默认扫描路径

1、springboot启动,需要扫描路径下的class文件,生成bean的定义BeanDefinition并创建bean对象,默认扫描路径是启动类所在的目录下。


2、如图下面的测试案例:
在启动类所在包目录之外的类即使加了@Component,也无法被扫描到生成bean对象。


3、如果就是要扫描到启动类所在包目录之外的类,使其生成bean对象,可以在启动类上加@ComponentScan注解

引入第三方jar包(里面也有bean对象)

1、这里使用本地自己打包的jar包(SDK)作例子
2、准备一个测试的SDK:


在测试项目中引入该SDK:


3、进行测试验证


从上述图片可以看出,我们引入SDK之后进行使用,会报找不到该bean对象,奇怪,SDK的TestDemoBean类不是加了@Component注解吗?这就跟第一大点,springboot默认扫描路径有关系,SDK中的TestDemoBean路径是com.example.jardemo.xxxx, 而测试项目中的扫描路径是:com.example.csdn,自然springboot启动无法扫描到。
4、提出疑问


5、修改SDK,添加配置文件spring.factories


把重新打包放入仓库中,记得重新加载一下引入的jar包,查看如下出现spring.factories文件:


温馨提醒:有的人可能在打包SDK的时候,发现resource目录下的文件没有打包进去,请修改pom.xml文件配置:


6、重新查看测试代码:

小结

1、上述内容主要是在说:提供SDK的时候,为了方便别人可以使用本SDK的bean对象,需要在其resource/META-INF目录下创建spring.factories文件,使用EnableAutoConfiguration 把需要变成bean对象的类变成bean对象。
2、这样子其他项目再引用该SDK的时候,不用在启动类中编写需要扫描的路
3、其实配置类可以使用@ConfigurationProperties+@Component:但是因为如下的原因:
(1)我们知道这种方式是spring在启动的时候来扫描该类上有@Component的时候,才会把它变成bean对象,又因为是在SDK(第三方jar包),因此需要在spring.factories中EnableAutoConfiguration加上该配置类路径,使其可以被spring扫描到
(2)所以存在一个问题,如果配置项很多了,岂不是你这个spring.factories中EnableAutoConfiguration加上该类的路径巨多,维护也不方便呀,如下:


因此,该配置类我们可以使用@EnableConfigurationProperties呀,在需要使用该配置类的上面加上这个注解,会自动把该@EnableConfigurationProperties注解指定的类对象,创建bean对象,这样子就无法在写到 spring.factories上面了:


4、重新打包,试一下呢:

总结

可以简单地理解:
@ConfigurationProperties+@Component = @EnableConfigurationProperties,深究的铁子们可以动手操作一下,加深理解与印象。在工作中,没有固定说使用哪一个。根据个人习惯使用即可。这里是"安前码后",一个专注分享实用干货的号,更多内容持续更新中。

猜你喜欢

转载自blog.csdn.net/weixin_42329623/article/details/131507421