1 JavaBeans、测试类
同上
2 修改
2.1 新建配置文件 person.properties
person.last-name=yangshuo
person.age=23
person.boss=false
person.birth=1995/12/07
person.maps.k1=v1
person.maps.k2=v2
person.lists=[yang1,yang2]
person.dog.name=dog1
person.dog.age=11
2.2 修改 Person
,添加 @PropertySource
注解
package com.snow.bean;
import lombok.Data;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
@PropertySource(value = "classpath:person.properties")
@Component
@ConfigurationProperties(prefix = "person")
@Data
@ToString
public class Person {
private String lastName;
private Integer age;
private Boolean boss;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
}
2.3 启动测试类,控制台打印
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.4.RELEASE)
2020-02-15 15:03:07.123 INFO 83853 --- [ main] c.s.s.SpringbootConfigAppTests : Starting SpringbootConfigAppTests on yangshuodembp with PID 83853 (started by yangshuo in /Users/yangshuo/Desktop/snow_springboot)
2020-02-15 15:03:07.124 INFO 83853 --- [ main] c.s.s.SpringbootConfigAppTests : No active profile set, falling back to default profiles: default
2020-02-15 15:03:09.352 INFO 83853 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-02-15 15:03:09.738 INFO 83853 --- [ main] c.s.s.SpringbootConfigAppTests : Started SpringbootConfigAppTests in 3.424 seconds (JVM running for 5.215)
Person(lastName=yangshuo, age=23, boss=false, birth=Thu Dec 07 00:00:00 CST 1995, maps={k1=v1, k2=v2}, lists=[[yang1, yang2]], dog=Dog(name=dog1, age=11))
2020-02-15 15:03:10.107 INFO 83853 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'