主要内容
配置文件的自定义属性的值注入问题
1 配置文件值注入
首先定义两个实体类
Dog.java
package com.wuk.helloworld.entity;
public class Dog {
private String name;
private Integer age;
public synchronized String getName() {
return name;
}
public synchronized void setName(String name) {
this.name = name;
}
public synchronized Integer getAge() {
return age;
}
public synchronized void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Dog [name=" + name + ", age=" + age + "]";
}
}
Person.java
package com.wuk.helloworld.entity;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/*
* 将配置文件中的每一个属性值,映射到这个组件中
* @ConfigurationProperties告诉springboot将本类中的所有属性和配置文件相关的的配置进行绑定
* prefix="person"通知与配置文件下面的哪个进行绑定
* @Component将该组件加入到容器中,才能使用容器所提供的@ConfigurationProperties功能
*/
@Component
@ConfigurationProperties(prefix="person")
public class Person {
private String lastname;
private Integer age;
private Date date;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
public synchronized String getLastname() {
return lastname;
}
public synchronized void setLastname(String lastname) {
this.lastname = lastname;
}
public synchronized Integer getAge() {
return age;
}
public synchronized void setAge(Integer age) {
this.age = age;
}
public synchronized Date getDate() {
return date;
}
public synchronized void setDate(Date date) {
this.date = date;
}
public synchronized Map<String, Object> getMaps() {
return maps;
}
public synchronized void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
public synchronized List<Object> getLists() {
return lists;
}
public synchronized void setLists(List<Object> lists) {
this.lists = lists;
}
public synchronized Dog getDog() {
return dog;
}
public synchronized void setDog(Dog dog) {
this.dog = dog;
}
@Override
public String toString() {
return "Person [lastname=" + lastname + ", age=" + age + ", date=" + date + ", maps=" + maps + ", lists="
+ lists + ", dog=" + dog + "]";
}
}
注意:
(1)@ConfigurationProperties告诉springboot将本类中的所有属性和配置文件相关的的配置进行绑定。
(2) prefix=”person”通知与配置文件下面的哪个进行绑定。
(3)@Component将该组件加入到容器中,才能使用容器所提供的@ConfigurationProperties功能。
然后写yml配置文件application.yml
#注释
person:
lastname: wuk
age: 24
date: 2017/07/07
maps: {k1: v1,k2: v2}
lists: [l1,l2,l3]
dog: {name: jack,age: 3 }
注意上述字符串都不用加双引号,键与值之间要有空格。
注意日期的格式只能是上述的字符串形式。
springboot测试类
package com.wuk.helloworld;
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;
import com.wuk.helloworld.entity.Person;
/*
* springboot的单元测试
* 可以在测试期间很方便的类似编码一样进行自动注入
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class HelloworldApplicationTests {
@Autowired
private Person person;
@Test
public void contextLoads() {
System.out.println(person);
}
}
输出结果:
Person [lastname=wuk, age=24, date=Fri Jul 07 00:00:00 CST 2017, maps={k1=v1, k2=v2}, lists=[l1, l2, l3], dog=Dog [name=jack, age=3]]
最后要注意的是:
导入配置文件处理器,这样以后再写自定义配置时候就有提示了。
<!‐‐导入配置文件处理器,配置文件进行绑定就会有提示‐‐>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐configuration‐processor</artifactId>
<optional>true</optional>
</dependency>