我是靠谱客的博主 犹豫书包,这篇文章主要介绍SpringBoot如何获取application.properties中自定义的值,现在分享给大家,希望可以做个参考。

目录结构:

pom文件:

复制代码
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
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.4</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>i18nSpringbootDemo-1</artifactId> <version>0.0.1-SNAPSHOT</version> <name>i18nSpringbootDemo-1</name> <description>Demo project for Spring Boot</description> <properties> <java.version>11</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- 导入配置文件处理器,配置文件进行绑定就会提示 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <!--校验依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <packaging>war</packaging> </project>

 application.properties:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
test.user.id=12 #也可以写成 test.user.user-name=zhangsan test.user.userName=zhansan #也可以写成 test.user.user-password=XXX test.user.userPassword=PWD123 #也可以写成 test.user.la-big-decimal=XXX test.user.laBigDecimal=138.3 test.user.maps.key1=V1 test.user.maps.key2=123 test.user.lists=a12,a13,sdf test.user.department.dep-code=dep001 test.user.department.dep-name=depName001

Department类:

复制代码
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
package com.example.demo.obj; public class Department { private String depCode; private String depName; /** * @return depCode */ public String getDepCode() { return depCode; } /** * @param depCode セットする depCode */ public void setDepCode(String depCode) { this.depCode = depCode; } /** * @return depName */ public String getDepName() { return depName; } /** * @param depName セットする depName */ public void setDepName(String depName) { this.depName = depName; } @Override public String toString() { return "Department [depCode=" + depCode + ", depName=" + depName + "]"; } }

User类:

复制代码
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package com.example.demo.obj; import java.math.BigDecimal; import java.util.List; import java.util.Map; import javax.validation.constraints.Email; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import org.springframework.validation.annotation.Validated; /* * 将配置文件的每一个属性值,映射到这个组件中: * ①@ConfigurationProperties:告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定; * prefix = "test.user":将配置文件中前缀为test.user下面的所有属性进行一一映射 * 只有这个组件是容器中的组件,才能提供@ConfigurationProperties的功能,所以要加@Component * * ②@Value("${key}")从环境变量、配置文件中获取值 * @Value("#{SpEl}")表达式 * * @ConfigurationProperties与@Value的区别: * @ConfigurationProperties支持松散语法,JSR303数据校验,复杂类型封装,不支持SpEL * @Value支持SpEL,不支持松散语法,JSR303数据校验,复杂类型封装 * 如果说,我们在某个业务逻辑中需要获取一下配置文件中的某项值,可以用@Value * 如果说,我们专门编写了一个javaBean去和配置文件进行映射,我们直接使用@ConfigurationProperties */ @Component @ConfigurationProperties(prefix = "test.user") @Validated public class User { //@Value("#{10*2}") private Integer id; //@Email userName必须输入邮箱格式的值,要不然报错 //@Value("${test.user.userName}") private String userName; //@Value("${test.user.userPassword}") private String userPassword; //@Value("${test.user.laBigDecimal}") private BigDecimal laBigDecimal; //@Value("${test.user.maps}") X 不行会报错 private Map<String, Object> maps; //@Value("${test.user.lists}") private List<Object> lists; //@Value("${test.user.department}") X 不行会报错 private Department department; /** * @return id */ public Integer getId() { return id; } /** * @param id セットする id */ public void setId(Integer id) { this.id = id; } /** * @return userName */ public String getUserName() { return userName; } /** * @param userName セットする userName */ public void setUserName(String userName) { this.userName = userName; } /** * @return userPassword */ public String getUserPassword() { return userPassword; } /** * @param userPassword セットする userPassword */ public void setUserPassword(String userPassword) { this.userPassword = userPassword; } /** * @return laBigDecimal */ public BigDecimal getLaBigDecimal() { return laBigDecimal; } /** * @param laBigDecimal セットする laBigDecimal */ public void setLaBigDecimal(BigDecimal laBigDecimal) { this.laBigDecimal = laBigDecimal; } /** * @return maps */ public Map<String, Object> getMaps() { return maps; } /** * @param maps セットする maps */ public void setMaps(Map<String, Object> maps) { this.maps = maps; } /** * @return lists */ public List<Object> getLists() { return lists; } /** * @param lists セットする lists */ public void setLists(List<Object> lists) { this.lists = lists; } /** * @return department */ public Department getDepartment() { return department; } /** * @param department セットする department */ public void setDepartment(Department department) { this.department = department; } @Override public String toString() { return "User [id=" + id + ", userName=" + userName + ", userPassword=" + userPassword + ", laBigDecimal=" + laBigDecimal + ", maps=" + maps + ", lists=" + lists + ", department=" + department + "]"; } }

I18nSpringbootDemo1Application类:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * 应用启动类 * */ @SpringBootApplication public class I18nSpringbootDemo1Application { public static void main(String[] args) { SpringApplication.run(I18nSpringbootDemo1Application.class, args); } }

单元测试类I18nSpringbootDemo1ApplicationTests:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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.obj.User; @SpringBootTest class I18nSpringbootDemo1ApplicationTests { @Autowired User user; @Test void contextLoads() { System.out.println(user.toString()); } }

 启动:

结果:
User [id=12, userName=zhansan, userPassword=PWD123, laBigDecimal=138.3, maps={key1=V1, key2=123}, lists=[a12, a13, sdf], department=Department [depCode=dep001, depName=depName001]]

到此这篇关于SpringBoot获取application.properties中的自定义的值的文章就介绍到这了,更多相关SpringBoot获取自定义值内容请搜索靠谱客以前的文章或继续浏览下面的相关文章希望大家以后多多支持靠谱客!

最后

以上就是犹豫书包最近收集整理的关于SpringBoot如何获取application.properties中自定义的值的全部内容,更多相关SpringBoot如何获取application.properties中自定义内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(106)

评论列表共有 0 条评论

立即
投稿
返回
顶部