-
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; }
-
配置文件
person.last-name=李四 person.age=30 person.birth=1989/9/12 person.boss=false person.maps.k1=v1 person.maps.k2=v2 person.lists=one,two,three person.dog.name=大黄 person.dog.age=2
-
测试
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); } }
-
properties配置文件在idea中默认utf-8出现乱码
- 解决