spring boot啟動(dòng)時(shí)加載外部配置文件的方法
前言
相信很多人選擇Spring Boot主要是考慮到它既能兼顧Spring的強(qiáng)大功能,還能實(shí)現(xiàn)快速開(kāi)發(fā)的便捷。本文主要給大家介紹了關(guān)于spring boot啟動(dòng)時(shí)加載外部配置文件的相關(guān)內(nèi)容,下面話不多說(shuō)了,來(lái)隨著小編一起學(xué)習(xí)學(xué)習(xí)吧。
業(yè)務(wù)需求:
加載外部配置文件,部署時(shí)更改比較方便。
先上代碼:
@SpringBootApplication
public class Application {
public static void main(String[] args) throws Exception {
SpringApplicationBuilder springApplicationBuilder = new SpringApplicationBuilder(Application.class);
springApplicationBuilder.web(true);
Properties properties = getProperties();
StandardEnvironment environment = new StandardEnvironment();
environment.getPropertySources().addLast(new PropertiesPropertySource("micro-service", properties));
springApplicationBuilder.environment(environment);
springApplicationBuilder.run(args);
}
private static Properties getProperties() throws IOException {
PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
propertiesFactoryBean.setIgnoreResourceNotFound(true);
Resource fileSystemResource = resolver.getResource("file:/opt/company/test.properties");
propertiesFactoryBean.setLocations(fileSystemResource);
propertiesFactoryBean.afterPropertiesSet();
return propertiesFactoryBean.getObject();
}
}
使用變量的工具類
@Component
public class EnvironmentUtil {
private static Environment environment;
@Autowired
public void setEnvironment(Environment environment) {
EnvironmentUtil.environment = environment;
}
public static <T> T getProperty(String key, Class<T> targetType, T defaultValue) {
return environment.getProperty(key, targetType, defaultValue);
}
public static <T> T getProperty(String key, Class<T> targetType) {
return environment.getProperty(key, targetType);
}
public static String getProperty(String key) {
return environment.getProperty(key);
}
public static String getProperty(String key, String defaultValue) {
return environment.getProperty(key, defaultValue);
}
public static Integer getInteger(String key, Integer defaultValue) {
return environment.getProperty(key, Integer.class, defaultValue);
}
}
也可以通過(guò)@Value("${key}")使用
這中加載方法優(yōu)先級(jí)很高,如果與spring boot配置文件同名,將覆蓋application.properties文件中的配置。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- 淺談SpringBoot2.4 配置文件加載機(jī)制大變化
- SpringBoot內(nèi)部外部配置文件加載順序解析
- 詳解springboot啟動(dòng)時(shí)是如何加載配置文件application.yml文件
- 簡(jiǎn)單了解springboot加載配置文件順序
- Spring Boot加載配置文件的完整步驟
- Springboot為什么加載不上application.yml的配置文件
- SpringBoot配置文件的加載位置實(shí)例詳解
- spring boot加載第三方j(luò)ar包的配置文件的方法
- 詳解Spring Boot加載properties和yml配置文件
- 在Spring Boot中從類路徑加載文件的示例
相關(guān)文章
Springboot使用test無(wú)法啟動(dòng)問(wèn)題的解決
這篇文章主要介紹了Springboot使用test無(wú)法啟動(dòng)問(wèn)題的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
Spring+MyBatis多數(shù)據(jù)源配置實(shí)現(xiàn)示例
本篇文章主要介紹了Spring+MyBatis多數(shù)據(jù)源配置實(shí)現(xiàn)示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01
Spring boot搭建web應(yīng)用集成thymeleaf模板實(shí)現(xiàn)登陸
這篇文章主要介紹了Spring boot搭建web應(yīng)用集成thymeleaf模板實(shí)現(xiàn)登陸,頁(yè)面使用bootstrap,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
歸并算法之有序數(shù)組合并算法實(shí)現(xiàn)
這篇文章主要介紹了歸并算法之有序數(shù)組合并算法實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2017-07-07
Java異常 Factory method''sqlSessionFactory''rew exception;este
這篇文章主要介紹了Java異常 Factory method ‘sqlSessionFactory‘ threw exception; nested exception is java.lang.NoSuchMethodError:,本文介紹了springboot 引入mybatis-plus后報(bào)錯(cuò)的解決方案,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
springboot之端口設(shè)置和contextpath的配置方式
這篇文章主要介紹了springboot之端口設(shè)置和contextpath的配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
MyBatis解決Update動(dòng)態(tài)SQL逗號(hào)的問(wèn)題
這篇文章主要介紹了MyBatis解決Update動(dòng)態(tài)SQL逗號(hào)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01

