美文网首页
@EnableConfigurationProperties 注

@EnableConfigurationProperties 注

作者: f0cf20ca7354 | 来源:发表于2019-05-29 09:30 被阅读0次

    @EnableConfigurationProperties注解的作用是:使使用 @ConfigurationProperties 注解的类生效。

    说明:

    如果一个配置类只配置@ConfigurationProperties注解,而没有使用@Component,那么在IOC容器中是获取不到properties 配置文件转化的bean。说白了 @EnableConfigurationProperties 相当于把使用  @ConfigurationProperties 的类进行了一次注入。

    测试发现 @ConfigurationProperties 与 @EnableConfigurationProperties 关系特别大。

    测试证明:

    @ConfigurationProperties与@EnableConfigurationProperties的关系。

    @EnableConfigurationProperties文档中解释:

    当@EnableConfigurationProperties注解应用到你的@Configuration时, 任何被@ConfigurationProperties注解的beans将自动被Environment属性配置。 这种风格的配置特别适合与SpringApplication的外部YAML配置进行配合使用。

    测试发现:

    1.使用@EnableConfigurationProperties进行注册

    自动配置设置

    一切正常,但是 HelloServiceAutoConfiguration 头部不使用@EnableConfigurationProperties,测访问报错。

    2.不使用@EnableConfigurationProperties进行注册,使用@Component注册

    Controller 不变,一切正常,如果注释掉 @Component 测启动报错。

    由此证明,两种方式都是将被  @ConfigurationProperties 修饰的类,加载到 Spring Env 中。

    相关文章

      网友评论

          本文标题:@EnableConfigurationProperties 注

          本文链接:https://www.haomeiwen.com/subject/ouzptctx.html