环境:使用person.properties文件
# person.properties使用在参数过多容易眼花,所以分类到不同文件
person.name=现在
person.isBooss=false
person.birthday=2019/8/06
person.map.k1=v1
。。。测试就设置所有参数了
一、 @PropertySource
@PropertySource(value = "classpath:person.properties")
@ConfigurationProperties(prefix = "person")
@Component
public class Person {
private String name;
private boolean isBooss;
private Date birthday;
private Map map;
private List list;
private Dog dog;
@Value("#{10+20}")
private int age;
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", isBooss=" + isBooss +
", birthday=" + birthday +
", map=" + map +
", list=" + list +
", dog=" + dog +
", age=" + age +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Boolean getBooss() {
return isBooss;
}
public void setBooss(Boolean booss) {
isBooss = booss;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
}
二、@ImportResource
①applicationContext.xml
<?xml version="1.0" encoding="UTF‐8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema‐instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring‐beans.xsd">
<bean id="helloService" class="com.pringboot.service.HelloService"></bean>
</beans>
②代码
@ImportResource(locations = {
"classpath:applicationContext.xml"})
@SpringBootApplication
public class SpringBootDemo01Application {
public static void main(String[] args) {
SpringApplication.run(SpringBootDemo01Application.class, args);
}
}
三、@Configuration
详情可参考 这里