美文网首页
springBoot @PropertySource读取配置文件

springBoot @PropertySource读取配置文件

作者: EricDD | 来源:发表于2018-07-08 09:48 被阅读0次
  1. spring-boot 1.5 之后 @ConfigurationProperties 注解取消 locations 属性。因此读取其他配置文件内配置项,使用@PropertySource配合使用。
  2. @PropertySource只支持properties 文件 不支持 yaml文件。
  3. @PropertySource使用并不影响@ConfigurationProperties。

示例代码:

@Data
@Component
@ConfigurationProperties(prefix = "person")
// 指定配置文件和编码
@PropertySource(value = "classpath:person.properties",encoding = "utf-8") 
public class Person {
    private String name;
    private String fullName;
    private Integer age;
    private List<String> list;
    private Map<String,String> map;
    private Dog dog;
}
person.name=xiaoming
person.full-name=小明
person.age=11
person.list[0]=a
person.list[1]=b
person.list[2]=c
person.list[3]=d
person.map.key1=value1
person.map.key2=value2
person.dog.name=tom
person.dog.age=3
@Autowired
private Person person;

@GetMapping("/getPerson")
public Person getPerson(){
的    return person;
}
结果.png

相关文章

网友评论

      本文标题:springBoot @PropertySource读取配置文件

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