@ConfigurationProperties
@ConfigurationProperties
是springboot中注解,用于将主配置文件(application.properties或者“application.yml” )中的属性,映射到实体类中对应的属性,同时,也可以通过指定prefix方式,进行组件初始化使用。
常见使用方式:
1 | //通过下面两个注解把注解类注册、初始化、并注入到容器中 |
@ConfigurationProperties
是针对主配置文件数据来配合组件初始化。
@PropertySource
@PropertySource
是spring的注解,目的是加载指定位置文件配置到容器中。可以配合 @Value 和 @ConfigurationProperties 使用。
用法:
1 | + +Environment |
类路径下的hellword.properties
1 | helloworld = |
@Configuration+ @PropertySource+Environment
1 |
|
@Configuration+ @PropertySource+@Value
1 |
|
@Configuration+ @PropertySource+@ConfigurationProperties
@PropertySource指定加载哪个文件,@ConfigurationProperties指定加载文件中的哪一类属性。
@PropertySource+@ConfigurationProperties在一起解决了@ConfigurationProperties只能加载主文件内属性问题。
1 |
|
补充:
@Import(类.class)
给被注解类增加指定组件。
@ImportResource
:注解的文件格式和内容应该是xml文件,@ImportResource:注解的文件格式和内容应该是xml文件。