springboot读取配置文件到静态工具类
通常我们读取配置文件可以用@Value注解和@Configuration,@ConfigurationProperties(prefix = "xxx")等注解,但是这种方式是无法把配置读取到静态变量的,如果我们想在项目初始化时把配置文件加载到一个工具类,然后通过静态变量的方式调用的话我们就不能使用这两种方法。
这时候,我们可以用Environment 来解决。
不废话了,直接上代码
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
/**
*
* @Description: 配置常量类——根据不同的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");
}
}
我写完以后发现有些麻烦,下面是改进的方法,不需要每个配置都去get一下,只需要把配置文件的key与工具类的静态变量名写成一样的即可。
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: 配置常量类——根据不同的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, 单测调代码的时候不方便,所以又写了一个不依赖spring的版本。
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.util.Properties;
/**
*
* @Description: 配置常量类——根据不同的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();
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)