Spring Boot里面所有的配置信息都放在application.properties中,如果我們想讀取配置中的值要怎么做呢?
還需要自己寫個讀取屬性文件的工具類嗎?完全不要,我們可以通過各種方式來讀取里面的值。
當然寫工具類也是一種方式,只是太麻煩了,既然Spring Boot中有封裝好的實現(xiàn),為什么不用。
Environment方式讀取
框架中有一個org.springframework.core.env.Environment類,可以讀取application.properties中配置的值。
用法如下,我們可以看到直接將Environment注入進來,然后就可以使用getProperty方法來獲取配置的值了,參數(shù)是配置的名稱。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
@RestController public class ConfigController { @Autowired private Environment env; /** * 通過配置的key獲取value<br> * {key:.+}是為了解決通過url參數(shù)訪問的時候小數(shù)點丟失的問題 * @param key * @return */ @RequestMapping ( "/config/{key:.+}" ) Object getConfig( @PathVariable String key) { return env.getProperty(key); } } |
我們獲取下之前配置的tomcat端口,http://localhost/spring-boot/config/server.port可以看到輸出的結果正是你配置的值。
@Value注解方式讀取
用法如下,通過注解的方式將要讀取的值映射到這個字段上面,然后就可以直接使用了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
@RestController public class ConfigController { /** * 讀取application.properties中的配置值 */ @Value ( "${server.context-path}" ) private String contextPath; @RequestMapping ( "/config/contextpath" ) Object getConfigContextPath() { return contextPath; } } |
獲取contextPath http://localhost/spring-boot/config/contextpath
自定義配置文件讀取方式
系統(tǒng)自帶的application.properties是配置一些框架相關的參數(shù),當我們有一些關于業(yè)務方面的配置,如果配置在application.properties中就有點不合適了,這個時候就需要自定義配置文件了。
在沒用Spring Boot之前也是建個屬性文件,然后里面配置好值,用工具類去讀取
當然也可以用Spring提供的PropertiesFactoryBean去讀取,現(xiàn)在讀取就更簡單了
這邊可以直接將配置信息映射成實體類,方便使用,首先定義個配置實體類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
@ConfigurationProperties (locations = "classpath:config.properties" , prefix = "config" ) @Component public class Config { @NotEmpty private String ip; private int port; public String getIp() { return ip; } public void setIp(String ip) { this .ip = ip; } public int getPort() { return port; } public void setPort( int port) { this .port = port; } } |
加上@Component和@ConfigurationProperties注解
@ConfigurationProperties中的locations用來指定你配置文件所在的路徑
@ConfigurationProperties中的prefix用來指定你配置名稱的前綴,如config.ip, config就是你上面定義的前綴
@ConfigurationProperties注解用的特別多,在很多starter包中都使用到了,比
如說mongodb的配置類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@ConfigurationProperties (prefix = "spring.data.mongodb" ) public class MongoProperties { /** * Default port used when the configured port is {@code null}. */ public static final int DEFAULT_PORT = 27017 ; /** * Mongo server host. */ private String host; /** * Mongo server port. */ private Integer port = null ; // .... } |
這邊在ip字段上還加了個@NotEmpty注解來防止忘記配置值了,如果你沒配置ip的值,那么在啟動的程序的時候框架將提示你
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
|
*************************** APPLICATION FAILED TO START *************************** Description: Binding to target com.cxytiandi.config.Config @2af616d3 failed: Property: config.ip Value: null Reason: 不能為空 Action: Update your application's configuration 然后我們創(chuàng)建個config.properties放在classpath下 config.ip= 192.168 . 1.1 config.port= 8080 使用就直接注入Config類就行了 @RestController public class ConfigController { @Autowired private Config config; @RequestMapping ( "/config" ) Object queryConfig() { return config; } } |
這邊通過地址獲取下配置信息:http://localhost/spring-boot/config 可以看到結果
{"ip":"192.168.1.1","port":8080}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.51cto.com/14888386/2516387