@ConfigurationProperties,@PropertySource,@ImportResource注解用法
@ConfigurationProperties
将配置文件中配置的每一个属性的值,映射到这个组件中
@ConfigurationProperties:
告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定;
prefix = “person”:配置文件中哪个下面的所有属性进行一一映射
只有这个组件是容器中的组件,才能容器提供的@ConfigurationProperties功能;
@ConfigurationProperties(prefix = “person”)默认从全局配置文件中获取值;
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String lastName;
private Integer age;
private Boolean boss;
private Date birth;
@PropertySource
为减少全局配置文件中的无关配置,创建一个普通的properties文件存放相关配置信息
此注解的作用就是引入这个普通的properties文件,将配置信息注入
@PropertySource(value = {"classpath:person.properties"})
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String lastName;
private Integer age;
private Boolean boss;
private Date birth;
@ImportResource:
导入Spring的配置文件,让配置文件里面的内容生效;
Spring Boot里面没有Spring的配置文件,我们自己编写的配置文件,也不能自动识别; 想让Spring的配置文件生效,加载进来;
@ImportResource标注在一个配置类上
/**
* @ImportResource 作用:导入spring的配置文件,让配置文件里面的内容生效
*/
@ImportResource({"classpath:beans.xml"})
@SpringBootApplication
public class SpringBoot02ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBoot02ConfigApplication.class, args);
}
}
努力进步为了自己,也为了父母妻儿