10.SpringBoot的两种配置文件语法

 

解决提示问题

添加Spring Configuartion Processor依赖

Demo

@ConfigurationProperties(prefix=”pre”)

Springboot标记了IOC容器里面一个对象

当IOC里面的对象初始化完成之后,再去扫描@ConfigurationProperties

然后把配置文件里面是pre前缀开头的配置注入到IOC这个对象的相应的set方法里面

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.example.demo.vo;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

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

@Component // <bean id="student" class="com.example.vo.Student"></bean>
@ConfigurationProperties(prefix = "student")
public class Student {

private Integer id;
private String userName;
private Integer age;
private String[] hobby;
private List<String> course;
private Set<String> book;
private Map<String, String> score;
private Date birthday;

public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String[] getHobby() {
return hobby;
}
public void setHobby(String[] hobby) {
this.hobby = hobby;
}
public List<String> getCourse() {
return course;
}
public void setCourse(List<String> course) {
this.course = course;
}
public Set<String> getBook() {
return book;
}
public void setBook(Set<String> book) {
this.book = book;
}
public Map<String, String> getScore() {
return score;
}
public void setScore(Map<String, String> score) {
this.score = score;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}

@Override
public String toString() {
return "Student [id=" + id + ", userName=" + userName + ", age=" + age + ", hobby=" + Arrays.toString(hobby)
+ ", course=" + course + ", book=" + book + ", score=" + score + ", birthday=" + birthday + "]";
}

}
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.example.demo.vo;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

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

@Component
@ConfigurationProperties(prefix = "person")
public class Person {

private Integer id;
private String userName;
private Integer age;
private String[] hobby;
private List<String> course;
private Set<String> book;
private Map<String, String> score;
private Date birthday;

public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String[] getHobby() {
return hobby;
}
public void setHobby(String[] hobby) {
this.hobby = hobby;
}
public List<String> getCourse() {
return course;
}
public void setCourse(List<String> course) {
this.course = course;
}
public Set<String> getBook() {
return book;
}
public void setBook(Set<String> book) {
this.book = book;
}
public Map<String, String> getScore() {
return score;
}
public void setScore(Map<String, String> score) {
this.score = score;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}

@Override
public String toString() {
return "Person [id=" + id + ", userName=" + userName + ", age=" + age + ", hobby=" + Arrays.toString(hobby)
+ ", course=" + course + ", book=" + book + ", score=" + score + ", birthday=" + birthday + "]";
}

}

使用application.properties的文件注入

1
2
3
4
5
6
7
8
9
10
11
12
13
server.port=9999

#config
student.id=1
student.user-name=xiaoming
student.age=10
student.hobby=reading, exercise, writing
student.course=literature, mathematics, geography, chemistry
student.book=book1, book2, book1, book2, book3
student.score.math=90
student.score.chemistry=85
#student.birthday.time=
student.birthday=2020/10/01 08:50:30

使用application.yml的文件注入

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
server:
port: 8890

#config
person:
id: 2
user-name: xiaozhang
age: ${random.int(1,100)}
hobby:
- reading
- exercise
- writing
course:
- literature
- mathematics
- geography
- chemistry
- ${course}
book:
- book1
- book2
- book3
- ${person.id}
score:
math: 90
chemistry: 85
birthday:
time: 1610890991736

course: biology
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
package com.example.demo;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import com.example.demo.vo.Person;
import com.example.demo.vo.Student;

@SpringBootTest
class SpringBoot007ApplicationTests {

@Autowired
private Student student;

@Autowired
private Person person;

@Test
public void testStudent() {
System.out.println(student);
// Student [id=1, userName=xiaoming, age=10, hobby=[reading, exercise, writing], course=[literature, mathematics, geography, chemistry], book=[book1, book2, book3], score={math=90, chemistry=85}, birthday=Thu Oct 01 08:50:30 CST 2020]
}

@Test
public void testPerson() {
System.out.println(person);
// Person [id=2, userName=xiaozhang, age=43, hobby=[reading, exercise, writing], course=[literature, mathematics, geography, chemistry, biology], book=[${person.course}], score={math=90, chemistry=85}, birthday=Sun Jan 17 21:43:11 CST 2021]
}

}

随机数

1
2
3
4
5
${random.int}: 生成一个int类型的随机数
${random.value}: 生成一个随机字符串
${random.long}: 生成一个long的随机数据
${random.int(10)}: 生成0-10的int类型的随机数
${random.int[10, 100]}: 生成10-100的int类型的随机数

IOC容器对象读取

1
${student.name}: 读取IOC容器里面的student对象里的name属性

两种方法的说明

  1. 如果properties里面配置了就不会去yml里面去取值,如果没有配置就会去yml里面去取

  2. 两种配置方法是互补的