-
配置文件
person: lastName: hello age: 18 boss: false birth: 2017/12/12 maps: { k1: v1, k2: v2} lists: - lisi - zhaoliu dog: name : 小狗 age : 12
-
JavaBean
/ * * 将配置文件中配置的每一个属性的值,映射到这个组件中 * @ConfigurationProperties:告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定; * prefix = "person":配置文件中哪个下面的所有属性进行一一映射 * 只有这个组件是容器中的组件,才能容器提供的@ConfigurationProperties功能; * / @Component @ConfigurationProperties(prefix = "person") 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; }
-
导入配置文件处理器,配置文件进行绑定就会有提示
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring‐boot‐configuration‐processor</artifactId> <optional>true</optional> </dependency>
-
测试
import com.xxx.po.Person; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest public class SpringBoot02InitApplicationTests { @Autowired Person person; @Test public void contextLoads() { System.out.println(person); } }
5. 测试结果