使用springboot在工具類中讀取配置文件(ClassPathResource)
springboot工具類中讀取配置文件
1、創(chuàng)建配置文件(application.properties)
spring.activemq.broker-url=tcp://localhost:61616 spring.activemq.user=admin spring.activemq.password=admin spring.activemq.in-memory=true spring.activemq.pool.enabled=false
2、創(chuàng)建工具類(PropertiesUtil.java)
package com.jeff.utils; import java.io.IOException; import java.util.Properties; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.support.PropertiesLoaderUtils; public class PropertiesUtil { private static String user; static { System.out.println("application.properties屬性文件讀取開始"); ClassPathResource resource = new ClassPathResource("application.properties"); try { Properties properties = PropertiesLoaderUtils.loadProperties(resource); user = properties.getProperty("spring.activemq.user"); System.out.println("user的值:" + user); } catch (IOException e) { System.out.println("application.properties屬性文件讀取異常" + e); } System.out.println("application.properties屬性文件讀取完成"); } public static String getUser() { System.out.println("獲取user的值:" + user); return user; } }
3、創(chuàng)建測(cè)試類(MyController.java)
package com.jeff.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.jeff.utils.PropertiesUtil; @RestController public class MyController { @RequestMapping("myTest") public String myTest() { PropertiesUtil.getUser(); return "success"; } }
4、打開瀏覽器訪問 http://localhost:8080/myTest,控制臺(tái)輸出結(jié)果
springboot讀取配置文件到靜態(tài)工具類
通常我們讀取配置文件可以用@Value注解和@Configuration,@ConfigurationProperties(prefix = "xxx")等注解,但是這種方式是無法把配置讀取到靜態(tài)變量的,如果我們想在項(xiàng)目初始化時(shí)把配置文件加載到一個(gè)工具類,然后通過靜態(tài)變量的方式調(diào)用的話我們就不能使用這兩種方法。
這時(shí)候,我們可以用Environment 來解決
不廢話了,直接上代碼
import javax.annotation.PostConstruct; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; import org.springframework.stereotype.Component; /** * * @Description: 配置常量類——根據(jù)不同的spring-profile加載不同的配置 * @author: eric.zhang * @date: 2018年7月20日 上午10:59:24 */ @Component public class ConfigConstant { @Autowired private Environment env; public static String url; public static String param; @PostConstruct public void readConfig() { url = env.getProperty("config.url"); param = env.getProperty("config.param"); } }
我寫完以后發(fā)現(xiàn)有些麻煩,下面是改進(jìn)的方法,不需要每個(gè)配置都去get一下,只需要把配置文件的key與工具類的靜態(tài)變量名寫成一樣的即可。
import java.io.UnsupportedEncodingException; import java.lang.reflect.Field; import javax.annotation.PostConstruct; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; import org.springframework.stereotype.Component; /** * * @Description: 配置常量類——根據(jù)不同的spring-profile加載不同的配置,變量名要與配置文件里寫的名一致 * @author: eric.zhang * @date: 2018年7月20日 上午10:59:24 */ @Component public class ConfigConstant { @Autowired private Environment env; public static String url; public static String name; @PostConstruct public void readConfig() throws Exception { String prefix = "config."; Field[] fields = ConfigConstant.class.getFields(); for(Field field : fields ){ field.set(null, getProperty(prefix + field.getName())); } } private String getProperty(String key) throws UnsupportedEncodingException { return new String(env.getProperty(key).getBytes("ISO-8859-1"), "UTF-8"); } }
大哥說這樣寫依賴spring, 單測(cè)調(diào)代碼的時(shí)候不方便,所以又寫了一個(gè)不依賴spring的版本。
import java.io.InputStreamReader; import java.lang.reflect.Field; import java.util.Properties; /** * * @Description: 配置常量類——根據(jù)不同的spring-profile加載不同的配置 * 變量名把配置文件的key中的"."替換成"_"命名 * @author: eric.zhang * @date: 2018年7月20日 上午10:59:24 */ public class ConfigConstant { public static String CONFIG_URL; public static String CONFIG_NAME; static { try { Properties props = new Properties(); props.load(new InputStreamReader( ConfigConstant.class.getClassLoader().getResourceAsStream("application.properties"), "UTF-8")); String profile = props.getProperty("spring.profiles.active"); String envFile = "application-" + profile + ".properties"; Properties envProps = new Properties(); envProps.load(new InputStreamReader( ConfigConstant.class.getClassLoader().getResourceAsStream(envFile), "UTF-8")); Field[] fields = ConfigConstant.class.getFields(); for (Field field : fields) { field.set(null, envProps.getProperty(field.getName().replace("_", ".").toLowerCase())); } } catch (Exception e) { e.printStackTrace(); } } }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- java運(yùn)行jar包提示?“XXX中沒有主清單屬性”?"找不到主類”兩種解決辦法
- java實(shí)現(xiàn)讀取jar包中配置文件的幾種方式
- java后臺(tái)啟動(dòng)jar包的一些命令匯總
- springboot中關(guān)于classpath:路徑使用及說明
- springboot打成jar后獲取classpath下文件失敗的解決方案
- 解決SpringBoot ClassPathResource的大坑(FileNotFoundException)
- spring boot加載資源路徑配置和classpath問題解決
- 怎樣將一個(gè)JAR包添加到Java應(yīng)用程序的Boot?Classpath中
相關(guān)文章
Java通過Process類Runtime.getRuntime().exec()執(zhí)行bat腳本程序
用Java編寫應(yīng)用時(shí),有時(shí)需要在程序中調(diào)用另一個(gè)現(xiàn)成的可執(zhí)行程序或系統(tǒng)命令,這篇文章主要給大家介紹了關(guān)于Java如何通過Process類Runtime.getRuntime().exec()執(zhí)行bat腳本程序的相關(guān)資料,需要的朋友可以參考下2024-01-01Java多線程實(shí)現(xiàn)簡(jiǎn)易微信發(fā)紅包的方法實(shí)例
這篇文章主要給大家介紹了關(guān)于Java多線程實(shí)現(xiàn)簡(jiǎn)易微信發(fā)紅包的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01基于Retrofit+Rxjava實(shí)現(xiàn)帶進(jìn)度顯示的下載文件
這篇文章主要為大家詳細(xì)介紹了基于Retrofit+Rxjava實(shí)現(xiàn)帶進(jìn)度顯示的下載文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Springboot整合hutool驗(yàn)證碼的實(shí)例代碼
在 Spring Boot 中,你可以將 Hutool 生成驗(yàn)證碼的功能集成到 RESTful API 接口中,這篇文章主要介紹了Springboot整合hutool驗(yàn)證碼,需要的朋友可以參考下2024-08-08idea創(chuàng)建Spring項(xiàng)目的方法步驟(圖文)
這篇文章主要介紹了idea創(chuàng)建Spring項(xiàng)目的方法步驟(圖文),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01