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中優(yōu)化if-else語(yǔ)句的七種方法
if-else語(yǔ)句是控制流程的基本工具,但過(guò)度使用會(huì)使代碼變得復(fù)雜且難以維護(hù),在SpringBoot , SpringCloud項(xiàng)目中,優(yōu)化if-else結(jié)構(gòu)變得尤為重要,本文將深入探討七種策略,旨在減少SpringBoot , SpringCloud項(xiàng)目中 if-else的使用,需要的朋友可以參考下2024-07-07
java自帶的工具Jstack截取進(jìn)程中的堆棧信息
本文給大家記錄的是java自帶的工具Jstack截取進(jìn)程中的堆棧信息的方法,非常的實(shí)用,有需要的小伙伴可以參考下。2016-01-01
java8實(shí)現(xiàn)List中對(duì)象屬性的去重方法
這篇文章主要介紹了java8實(shí)現(xiàn)List中對(duì)象屬性的去重方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
Java多線程實(shí)現(xiàn)聊天客戶端和服務(wù)器
這篇文章主要為大家詳細(xì)介紹了Java多線程聊天客戶端和服務(wù)器實(shí)現(xiàn)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
Springboot 項(xiàng)目讀取Resources目錄下的文件(推薦)
這篇文章主要介紹了Springboot 項(xiàng)目讀取Resources目錄下的文件,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
java啟動(dòng)如何設(shè)置JAR包內(nèi)存大小
這篇文章主要介紹了java啟動(dòng)如何設(shè)置JAR包內(nèi)存大小問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02

