美文网首页SpringBootMVCJava技术
关与 @EnableConfigurationPropertie

关与 @EnableConfigurationPropertie

作者: 咪雅先森 | 来源:发表于2018-07-26 15:11 被阅读0次

先说作用:

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

说明:

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

测试证明:
@ConfigurationProperties@EnableConfigurationProperties 的关系。

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

测试发现:
1.使用 @EnableConfigurationProperties 进行注册

@ConfigurationProperties(prefix = "service.properties")
public class HelloServiceProperties {
    private static final String SERVICE_NAME = "test-service";

    private String msg = SERVICE_NAME;
       set/get
}


@Configuration
@EnableConfigurationProperties(HelloServiceProperties.class)
@ConditionalOnClass(HelloService.class)
@ConditionalOnProperty(prefix = "hello", value = "enable", matchIfMissing = true)
public class HelloServiceAutoConfiguration {

}

@RestController
public class ConfigurationPropertiesController {

    @Autowired
    private HelloServiceProperties helloServiceProperties;

    @RequestMapping("/getObjectProperties")
    public Object getObjectProperties () {
        System.out.println(helloServiceProperties.getMsg());
        return myConfigTest.getProperties();
    }
}

自动配置设置

service.properties.name=my-test-name
service.properties.ip=192.168.1.1
service.user=kayle
service.port=8080

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

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

@ConfigurationProperties(prefix = "service.properties")
@Component
public class HelloServiceProperties {
    private static final String SERVICE_NAME = "test-service";

    private String msg = SERVICE_NAME;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

}

Controller 不变,一切正常,如果注释掉 @Component 测启动报错。
由此证明,两种方式都是将被 @ConfigurationProperties 修饰的类,加载到 Spring Env 中。

相关文章

  • 关与 @EnableConfigurationPropertie

    先说作用: @EnableConfigurationProperties注解的作用是:使使用 @Configura...

  • 关于 @EnableConfigurationPropertie

    先说作用: @EnableConfigurationProperties注解的作用是:使使用 @Configura...

  • 关与“写”

    最近,不!应该说已经很长一段时间,身边的同事每天一个个开了公众号,每天用文字记录着关于工作,关于生活的点...

  • 关与梦想

    摸爬滚打多年之后,从事了不知自己喜欢还是不喜欢、善长还是不善长的工作多年后,甚至达到了自己制定的目标后,猛...

  • 关与你

    关于你所有的一切 记忆中的那片天空 是梦中反复出现的青灰色 纸飞机在辽远的头顶划过 有白鲸探出头嬉笑 从未想过 如...

  • 开与关

    曾经有人说过这样一句话:世界对你关上了一扇门,它一定会在某个地方为你打开一扇窗。我要在后面加上一句话:你必须要懂得...

  • 开与关

    清早,起床妆扮,开门了 深夜,卸妆沐浴,关门了 开门的时候,确认是真开 关门的时候,就是真的关 关闭这扇大门,它叫...

  • 关与焦虑

    今天休息,在中国日报上读到了一篇关于焦虑的文章。标题是这样的,你焦虑的根本原因是急于过标配的人生,看到之后是深有同...

  • 年与关

    年与关 关这个字,在中文中有着特殊的意义。关口,关隘,海关,都意指重要的分界。“羌笛何须怨杨...

  • 关与暗恋

    其实我是个胆小的人就连喜欢你都默默的只在心里不曾孚于脸上,表于话语喜欢你就仅仅在心底就已经够了

网友评论

    本文标题:关与 @EnableConfigurationPropertie

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