application.properties大家都不陌生,我們在開發的時候,經常使用它來配置一些可以手動修改而且不用編譯的變量,這樣的作用在于,打成war包或者jar用于生產環境時,我們可以手動修改環境變量而不用再重新編譯。
spring boo默認已經配置了很多環境變量,例如,tomcat的默認端口是8080,項目的contextpath是“/”等等,可以在這里看spring boot默認的配置信息http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-external-config
spring boot允許你自定義一個application.properties文件,然后放在以下的地方,來重寫spring boot的環境變量或者定義你自己環境變量
- 當前目錄的 “/config”的子目錄下
- 當前目錄下
- classpath根目錄的“/config”包下
- classpath的根目錄下
1點和2點適合在生產環境下,例如,打包成可執行的jar包
這里要注意,“當前目錄”是指demo.jar包的目錄下,要使配置文件生效,在使用Java -jar demo.jar的命令時,必須先路由到demo.jar包的路徑下,再使用其命名,
3點和4點適合在開發環境下
如果同時在四個地方都有配置文件,配置文件的優先級是從1到4。
使用配置文件之后,spring boo啟動時,會自動把配置信息讀取到spring容器中,并覆蓋spring boot的默認配置,那么,我們怎么來讀取和設置這些配置信息呢
1.通過命令行來重寫和配置環境變量,優先級最高,例如可以通過下面的命令來重寫spring boot 內嵌tomcat的服務端口,注意“=”倆邊不要有空格
1
|
java -jar demo.jar --server.port=9000 |
如果想要設置多個變量怎么辦,可以已json的格式字符串來設置
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接口來獲取,只需要把接口注進去即可
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.可以自定義一個工具類,來獲取,這種方式關鍵在于讀取配置文件信息,適合自定義的配置信息,spring 容器默認的配置信息會讀不到
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配置隨機變量,利用的是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 ]} |
簡單的配置文件的使用就先寫到這里,再看看其他高級用法,如Profiles還有@ConfigurationProperties
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/yingxiake/article/details/51260302