/**
* @Project:
* @Author: liming
* @Date: 2018年06月06日
*/
package com.withmes.springbootdemoconfig02;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
* ClassName: Person
* @Description:
* @author liming
* @date 2018年06月06日
*/
@Component
@PropertySource(value ={"classpath:person.properties"}) //此处貌似只能用properties ,用yaml将获取不到值
@ConfigurationProperties(prefix = "person") //先用@PropertySource标签获取指定的配置文件,然后再用@ConfigurationProperties获取对应的key,先后顺序不能错
public class Person {
private String name;
private Integer age;
private Dog dog ;
private Map<String,Object> map;
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", dog=" + dog +
", map=" + map +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public Map<String, Object> getMap() {
return map;
}
public void setMap(Map<String, Object> map) {
this.map = map;
}
}
person.properties内容如下:
person.name=王大锤
结果能够正常获取到