application.properties大家都不陌生,我們?cè)陂_發(fā)的時(shí)候,經(jīng)常使用它來配置一些可以手動(dòng)修改而且不用編譯的變量,這樣的作用在于,打成war包或者jar用于生產(chǎn)環(huán)境時(shí),我們可以手動(dòng)修改環(huán)境變量而不用再重新編譯。
spring boo默認(rèn)已經(jīng)配置了很多環(huán)境變量,例如,tomcat的默認(rèn)端口是8080,項(xiàng)目的contextpath是“/”等等,可以在這里看spring boot默認(rèn)的配置信息http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-external-config
spring boot允許你自定義一個(gè)application.properties文件,然后放在以下的地方,來重寫spring boot的環(huán)境變量或者定義你自己環(huán)境變量
- 當(dāng)前目錄的 “/config”的子目錄下
- 當(dāng)前目錄下
- classpath根目錄的“/config”包下
- classpath的根目錄下
1點(diǎn)和2點(diǎn)適合在生產(chǎn)環(huán)境下,例如,打包成可執(zhí)行的jar包
這里要注意,“當(dāng)前目錄”是指demo.jar包的目錄下,要使配置文件生效,在使用Java -jar demo.jar的命令時(shí),必須先路由到demo.jar包的路徑下,再使用其命名,
3點(diǎn)和4點(diǎn)適合在開發(fā)環(huán)境下
如果同時(shí)在四個(gè)地方都有配置文件,配置文件的優(yōu)先級(jí)是從1到4。
使用配置文件之后,spring boo啟動(dòng)時(shí),會(huì)自動(dòng)把配置信息讀取到spring容器中,并覆蓋spring boot的默認(rèn)配置,那么,我們?cè)趺磥碜x取和設(shè)置這些配置信息呢
1.通過命令行來重寫和配置環(huán)境變量,優(yōu)先級(jí)最高,例如可以通過下面的命令來重寫spring boot 內(nèi)嵌tomcat的服務(wù)端口,注意“=”倆邊不要有空格
1
|
java -jar demo.jar --server.port=9000 |
如果想要設(shè)置多個(gè)變量怎么辦,可以已json的格式字符串來設(shè)置
1
|
java -jar demo.jar --spring.application.json= '{"foo":"bar"}' |
2.通過@value注解來讀取
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@RestController @RequestMapping ( "/task" ) public class TaskController { @Value ( "${connection.remoteAddress}" ) private String address; @RequestMapping (value = { "/" , "" }) public String hellTask( @Value ( "${connection.username}" )String name){ return "hello task !!" ; } } |
3.通過Environment接口來獲取,只需要把接口注進(jìn)去即可
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@RestController @RequestMapping ( "/task" ) public class TaskController { @Autowired Environment ev ; @Value ( "${connection.remoteAddress}" ) private String address; @RequestMapping (value = { "/" , "" }) public String hellTask( @Value ( "${connection.username}" )String name){ String password = ev.getProperty( "connection.password" ); return "hello task !!" ; } } |
4.可以自定義一個(gè)工具類,來獲取,這種方式關(guān)鍵在于讀取配置文件信息,適合自定義的配置信息,spring 容器默認(rèn)的配置信息會(huì)讀不到
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
|
@Component public class SystemConfig { private static Properties props ; public SystemConfig(){ try { Resource resource = new ClassPathResource( "/application.properties" ); // props = PropertiesLoaderUtils.loadProperties(resource); } catch (IOException e) { e.printStackTrace(); } } /** * 獲取屬性 * @param key * @return */ public static String getProperty(String key){ return props == null ? null : props.getProperty(key); } /** * 獲取屬性 * @param key 屬性key * @param defaultValue 屬性value * @return */ public static String getProperty(String key,String defaultValue){ return props == null ? null : props.getProperty(key, defaultValue); } /** * 獲取properyies屬性 * @return */ public static Properties getProperties(){ return props; } } //用的話,就直接這樣子 String value = SystemConfig.getProperty( "key" ); |
5.可以利用${…}在application.properties引用變量
1
2
|
myapp.name=spring myapp.desc=${myapp.name} nice |
6.可以在application.properties配置隨機(jī)變量,利用的是RandomValuePropertySource類
1
2
3
4
5
|
my.secret=${random.value} my.number=${random. int } my.bignumber=${random. long } my.number.less.than.ten=${random. int ( 10 )} my.number.in.range=${random. int [ 1024 , 65536 ]} |
簡單的配置文件的使用就先寫到這里,再看看其他高級(jí)用法,如Profiles還有@ConfigurationProperties
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/yingxiake/article/details/51260302