Spring Boot配置讀取實(shí)現(xiàn)方法解析
Spring Boot里面所有的配置信息都放在application.properties中,如果我們想讀取配置中的值要怎么做呢?
還需要自己寫個讀取屬性文件的工具類嗎?完全不要,我們可以通過各種方式來讀取里面的值。
當(dāng)然寫工具類也是一種方式,只是太麻煩了,既然Spring Boot中有封裝好的實(shí)現(xiàn),為什么不用。
Environment方式讀取
框架中有一個org.springframework.core.env.Environment類,可以讀取application.properties中配置的值。
用法如下,我們可以看到直接將Environment注入進(jìn)來,然后就可以使用getProperty方法來獲取配置的值了,參數(shù)是配置的名稱。
@RestControllerpublic class ConfigController { @Autowired private Environment env; /** * 通過配置的key獲取value<br> * {key:.+}是為了解決通過url參數(shù)訪問的時候小數(shù)點(diǎn)丟失的問題 * @param key * @return */ @RequestMapping('/config/{key:.+}') Object getConfig(@PathVariable String key) { return env.getProperty(key); }}
我們獲取下之前配置的tomcat端口,http://localhost/spring-boot/config/server.port可以看到輸出的結(jié)果正是你配置的值。
@Value注解方式讀取
用法如下,通過注解的方式將要讀取的值映射到這個字段上面,然后就可以直接使用了。
@RestControllerpublic 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是配置一些框架相關(guān)的參數(shù),當(dāng)我們有一些關(guān)于業(yè)務(wù)方面的配置,如果配置在application.properties中就有點(diǎn)不合適了,這個時候就需要自定義配置文件了。
在沒用Spring Boot之前也是建個屬性文件,然后里面配置好值,用工具類去讀取
當(dāng)然也可以用Spring提供的PropertiesFactoryBean去讀取,現(xiàn)在讀取就更簡單了
這邊可以直接將配置信息映射成實(shí)體類,方便使用,首先定義個配置實(shí)體類
@ConfigurationProperties(locations = 'classpath:config.properties', prefix = 'config')@Componentpublic 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的配置類:
@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的值,那么在啟動的程序的時候框架將提示你
***************************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.1config.port=8080使用就直接注入Config類就行了@RestControllerpublic class ConfigController { @Autowired private Config config; @RequestMapping('/config') Object queryConfig() { return config; }}
這邊通過地址獲取下配置信息:http://localhost/spring-boot/config 可以看到結(jié)果
{'ip':'192.168.1.1','port':8080}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. ajax請求添加自定義header參數(shù)代碼2. 基于javascript處理二進(jìn)制圖片流過程詳解3. Gitlab CI-CD自動化部署SpringBoot項(xiàng)目的方法步驟4. ASP基礎(chǔ)知識VBScript基本元素講解5. 解決android studio引用遠(yuǎn)程倉庫下載慢(JCenter下載慢)6. idea刪除項(xiàng)目的操作方法7. 使用python 計(jì)算百分位數(shù)實(shí)現(xiàn)數(shù)據(jù)分箱代碼8. Kotlin + Flow 實(shí)現(xiàn)Android 應(yīng)用初始化任務(wù)啟動庫9. 詳談ajax返回?cái)?shù)據(jù)成功 卻進(jìn)入error的方法10. axios和ajax的區(qū)別點(diǎn)總結(jié)
