使用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)建測試類(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,控制臺輸出結(jié)果


springboot讀取配置文件到靜態(tài)工具類
通常我們讀取配置文件可以用@Value注解和@Configuration,@ConfigurationProperties(prefix = "xxx")等注解,但是這種方式是無法把配置讀取到靜態(tài)變量的,如果我們想在項目初始化時把配置文件加載到一個工具類,然后通過靜態(tài)變量的方式調(diào)用的話我們就不能使用這兩種方法。
這時候,我們可以用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)有些麻煩,下面是改進的方法,不需要每個配置都去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, 單測調(diào)代碼的時候不方便,所以又寫了一個不依賴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();
}
}
}
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java通過Process類Runtime.getRuntime().exec()執(zhí)行bat腳本程序
用Java編寫應(yīng)用時,有時需要在程序中調(diào)用另一個現(xiàn)成的可執(zhí)行程序或系統(tǒng)命令,這篇文章主要給大家介紹了關(guān)于Java如何通過Process類Runtime.getRuntime().exec()執(zhí)行bat腳本程序的相關(guān)資料,需要的朋友可以參考下2024-01-01
Java多線程實現(xiàn)簡易微信發(fā)紅包的方法實例
這篇文章主要給大家介紹了關(guān)于Java多線程實現(xiàn)簡易微信發(fā)紅包的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
基于Retrofit+Rxjava實現(xiàn)帶進度顯示的下載文件
這篇文章主要為大家詳細(xì)介紹了基于Retrofit+Rxjava實現(xiàn)帶進度顯示的下載文件,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05
idea創(chuàng)建Spring項目的方法步驟(圖文)
這篇文章主要介紹了idea創(chuàng)建Spring項目的方法步驟(圖文),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01

