Spring Boot的@PropertySource使用

标题@PropertySource注解的使用

  • @PropertySource(value = {“classpath:parson.properties”})
  • 导入指定的配置文件注入值,配合@ConfigurationProperties一起使用

我的JAVA Bean

/**
 * 将配置文件中配置的每一个属性的值,映射到这个组件中
 * @ConfigurationProperties:告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定;
 * prefix = "person":配置文件中哪个下面的所有属性进行一一映射
 *
 * @ConfigurationProperties注解映射的是全局的配置文件
 * 只有这个组件是容器中的组件,才能容器提供的@ConfigurationProperties功能;
 *
 * @PropertySource(value = {"classpath:parson.properties"})
 * 导入指定的配置文件注入值,配合@ConfigurationProperties一起使用
 */
@Component
@PropertySource(value = {"classpath:parson.properties"})
@ConfigurationProperties(prefix = "parsons")
public class Parson {
    private String name;
    private int age;
    private String sex;
    private Map<String,Integer> map;
    private List<Integer> list;
}

parson.properties配置文件中的值,@ConfigurationProperties(prefix = “parsons”)这里指向映射的属性

parsons.name=赵盼
parsons.age=19
parsons.sex=女
parsons.map.k1=1
parsons.map.k2=2
parsons.list=1,2

你可能感兴趣的:(笔记)