spring boot读取配置文件
1)通过注入applicationcontext 或者 environment对象来读取配置文件里的配置信息。
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
|
package com.ivan.config.controller; import org.springframework.beans.factory.annotation.autowired; import org.springframework.context.applicationcontext; import org.springframework.core.env.environment; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.bind.annotation.restcontroller; @restcontroller public class configcontroller { @autowired applicationcontext context; @autowired environment environment; @requestmapping (value= "/config" , method={requestmethod.get}) public string getconfigcontent(){ string name = context.getenvironment().getproperty( "db.user.name" ); return name; } @requestmapping (value= "/configenv" , method={requestmethod.get}) public string getconfigenvironment(){ string name = environment.getproperty( "db.user.name" ); return name; } } |
2)通过@configurationproperties配合@propertysource读取配置文件里的配置信息。
1:通过@propertysource指定当前类里属性的配置文件地址,configurationproperties可以指定配置的前缀,@configuration用于定义一个配置类:
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
|
package com.ivan.config.entity; import org.springframework.boot.context.properties.configurationproperties; import org.springframework.context.annotation.configuration; import org.springframework.context.annotation.propertysource; @configuration @propertysource ( "classpath:config/druid.properties" ) @configurationproperties (prefix = "druid" ) public class druidconfig { private int initialsize; private int minidle; private int maxactive; private int maxwait; private string validationquery; private boolean testwhileidle; private boolean testonborrow; private boolean testonreturn; public int getinitialsize() { return initialsize; } public void setinitialsize( int initialsize) { this .initialsize = initialsize; } public int getminidle() { return minidle; } public void setminidle( int minidle) { this .minidle = minidle; } public int getmaxactive() { return maxactive; } public void setmaxactive( int maxactive) { this .maxactive = maxactive; } public int getmaxwait() { return maxwait; } public void setmaxwait( int maxwait) { this .maxwait = maxwait; } public string getvalidationquery() { return validationquery; } public void setvalidationquery(string validationquery) { this .validationquery = validationquery; } public boolean istestwhileidle() { return testwhileidle; } public void settestwhileidle( boolean testwhileidle) { this .testwhileidle = testwhileidle; } public boolean istestonborrow() { return testonborrow; } public void settestonborrow( boolean testonborrow) { this .testonborrow = testonborrow; } public boolean istestonreturn() { return testonreturn; } public void settestonreturn( boolean testonreturn) { this .testonreturn = testonreturn; } @override public string tostring() { return "druidconfig [initialsize=" + initialsize + ", minidle=" + minidle + ", maxactive=" + maxactive + ", maxwait=" + maxwait + ", validationquery=" + validationquery + ", testwhileidle=" + testwhileidle + ", testonborrow=" + testonborrow + ", testonreturn=" + testonreturn + "]" ; } } |
2:对应的配置文件:
1
2
3
4
5
6
7
8
|
druid.initialsize= 5 druid.minidle= 5 druid.maxactive= 20 druid.maxwait= 60000 druid.validationquery=select 'x' druid.testwhileidle= true druid.testonborrow= true druid.testonreturn= true |
3:在需要用到的类通过@autowired注入
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package com.ivan.config.controller; import org.springframework.beans.factory.annotation.autowired; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.bind.annotation.restcontroller; import com.ivan.config.entity.druidconfig; @restcontroller public class druidconfigcontroller { @autowired public druidconfig druidconfig; @requestmapping (value= "/druidconfig" , method={requestmethod.get}) public string getdruidconfig(){ return druidconfig.tostring(); } } |
3)通过@value注解
1:需要得到配置属性的类如下,可以在任何需要得到配置的地方用@value注解
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package com.ivan.config.entity; import org.springframework.beans.factory.annotation.value; import org.springframework.context.annotation.configuration; @configuration public class valuetest { @value ( "${db.user.name}" ) private string username; public string getusername() { return username; } public void setusername(string username) { this .username = username; } } |
2:测试controller类通过@autowired注入实体类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package com.ivan.config.controller; import org.springframework.beans.factory.annotation.autowired; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.bind.annotation.restcontroller; import com.ivan.config.entity.valuetest; @restcontroller public class valuecontroller { @autowired private valuetest value; @requestmapping (value= "/configvalue" , method={requestmethod.get}) public string getconfig(){ return value.getusername(); } } |
spring boot 配置文件优先级:
1:命令行参数。(以--开头的参数,比如可以设置:--server.port对同一套代码设置不同的参数)
2: 通过 system.getproperties() 获取的 java 系统参数。
3:操作系统环境变量(这解释了为什么你通过application.properties设置的user.name取的是系统的用户名了)
4:从 java:comp/env 得到的 jndi 属性。
5: 应用 jar 文件之外的属性文件(系统的application.properties文件)
6:应用 jar 文件内部的属性文件。
7: 在应用配置 java 类(包含“@configuration”注解的 java 类)中通过“@propertysource”注解声明的属性文件。
8: 通过“springapplication.setdefaultproperties”声明的默认属性。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://www.jianshu.com/p/6fcd33551532