Spring—使用类加载从配置文件中读取配置
引言
SpringBoot中可以使用@Value
直接resources目录下的配置文件中读取相关配置参数。但是非微服务模式下,可以使用类加载机制进行获取。
模板
public class PropertiesConfig {
private static Properties properties;
private PropertiesConfig() {}
public static Properties getInstance() throws Exception{
synchronized(Properties.class) {
if (properties == null) {
properties = new Properties();
try (InputStream in = PropertiesConfig.class.getClassLoader().getResourceAsStream("application.properties")) {
properties.load(in);
} catch (Exception e) {
logger.error("获取配置文件异常: {}", e.getMessage());
}
}
}
return properties;
}
}
烧不死的鸟就是凤凰
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET10 - 预览版1新功能体验(一)