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