本示例使用JDK1.8, Spring Boot 1.5.9.RELEASE。

项目的一些配置有时候会存放在.properties/.yaml文件中,这些配置有时候需要直接映射到我们的java bean当中,然后直接使用。一般会使用@Value注解来完成,但是,在面对复杂的属性结构,@Value 有时候不能满足我们的需要或者使用繁琐,这时候就可以使用@ConfigurationProperties来完成。

@Value获取属性文件值

在平时的Spring项目中,如果想获取.properteis文件中的值,会使用@Value注解来完成。

例如:
hello.properteis

1
hello.msg = Hello Wolrd

HelloProperties.java

1
2
3
4
5
6
@Component
@PropertySource("hello.properties")
public class HelloProperties {
@Value("${hello.msg}")
private String msg ;
}

@ConfigurationProperties

: @ConfigurationProperties 可以对 .propertie 和 *.yml,.yaml 文件进行支持.

1.属性文件

现有如下两个配置文件:
hello.properties

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#fields
hello.msg = Hello Wolrd

#object
hello.student.name = Du Liu
hello.student.age = 18

#list
hello.book[0].author = Jack
hello.book[0].name = Hello Java
hello.book[0].price = 12.99
#hello.book[1].author = Tom
hello.book[1].name = Java Hello
hello.book[1].price = 66.99

同样的文件结构在.yaml中
hello.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
hello:
yaml:
msg : Hello Wolrd2
student:
name: Du Liu
age: 18

book:
- author : Jack
name : Hello Java
price : 12.99
- author : Tome
name : Java Hello
price : 66.99

2.Java Bean

负责处理.properties的 HelloProperties.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
*
*/
package com.devnp.springbootconfigurationpropertiesdemo.properties;

import java.util.List;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import com.devnp.springbootconfigurationpropertiesdemo.vo.Book;
import com.devnp.springbootconfigurationpropertiesdemo.vo.Student;

/**
* @author duliu
*
*/
@Component
@ConfigurationProperties(prefix="hello")
@PropertySource("hello.properties")
public class HelloProperties {

private String msg ;

private Student student ;

private List<Book> book ;

public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}

public Student getStudent() {
return student;
}

public void setStudent(Student student) {
this.student = student;
}

public List<Book> getBook() {
return book;
}

public void setBook(List<Book> book) {
this.book = book;
}


}

在.yaml属性文件中不能使用@PropertySource,而是使用 YamlPropertiesFactoryBeanYamlMapFactoryBean来负载YAML作为Map, 所以先配置 YamlPropertiesFactoryBean

1
2
3
4
5
6
7
8
@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
yaml.setResources(new ClassPathResource("hello.yaml"));
propertySourcesPlaceholderConfigurer.setProperties(yaml.getObject());
return propertySourcesPlaceholderConfigurer;
}

负责处理.yaml的 HelloYaml.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
*
*/
package com.devnp.springbootconfigurationpropertiesdemo.properties;

import java.util.List;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import com.devnp.springbootconfigurationpropertiesdemo.vo.Book;
import com.devnp.springbootconfigurationpropertiesdemo.vo.Student;

/**
* @author duliu
*
*/
@Component
@ConfigurationProperties(prefix="hello.yaml")
public class HelloYaml {

private String msg ;

private Student student ;

private List<Book> book ;

public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}

public Student getStudent() {
return student;
}

public void setStudent(Student student) {
this.student = student;
}

public List<Book> getBook() {
return book;
}

public void setBook(List<Book> book) {
this.book = book;
}
}

对应的 Book.javaStudent.java 对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
*
*/
package com.devnp.springbootconfigurationpropertiesdemo.vo;

/**
* @author duliu
*
*/
public class Book {

private String author ;

private String name ;

private Double price ;

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Double getPrice() {
return price;
}

public void setPrice(Double price) {
this.price = price;
}

@Override
public String toString() {
return "Book [author=" + author + ", name=" + name + ", price=" + price + "]";
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
*
*/
package com.devnp.springbootconfigurationpropertiesdemo.vo;

/**
* @author duliu
*
*/
public class Student {

private String name ;

private Integer age ;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}

}

3.测试

SpringBootConfigurationpropertiesDemoApplicationTests.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.devnp.springbootconfigurationpropertiesdemo;

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.devnp.springbootconfigurationpropertiesdemo.properties.HelloProperties;
import com.devnp.springbootconfigurationpropertiesdemo.properties.HelloYaml;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootConfigurationpropertiesDemoApplicationTests {

@Autowired
private HelloProperties helloProperties ;

@Autowired
private HelloYaml helloYaml ;

@Test
public void contextLoads() {
}

@Test
public void testProperties() {
System.out.println("This is from hello.properties file.");

System.out.println(helloProperties.getMsg());

System.out.println(helloProperties.getStudent());

helloProperties.getBook().stream().forEach( book -> System.out.println(book));
}

@Test
public void testYaml() {

System.out.println("This is from hello.yaml file.");

System.out.println(helloYaml.getMsg());

System.out.println(helloYaml.getStudent());

helloYaml.getBook().stream().forEach( book -> System.out.println(book));
}


}

运行结果:

1
2
3
4
5
6
7
8
9
10
This is from hello.properties file.
Hello Wolrd
Student [name=Du Liu, age=18]
Book [author=Jack, name=Hello Java, price=12.99]
Book [author=null, name=Java Hello, price=66.99]
This is from hello.yaml file.
Hello Wolrd2
Student [name=Du Liu, age=18]
Book [author=Jack, name=Hello Java, price=12.99]
Book [author=Tome, name=Java Hello, price=66.99]

代码下载

spring-boot-configurationproperties-demo.zip