spring boot啟動時加載外部配置文件的方法
前言
相信很多人選擇Spring Boot主要是考慮到它既能兼顧Spring的強大功能,還能實現(xiàn)快速開發(fā)的便捷。本文主要給大家介紹了關(guān)于spring boot啟動時加載外部配置文件的相關(guān)內(nèi)容,下面話不多說了,來隨著小編一起學(xué)習(xí)學(xué)習(xí)吧。
業(yè)務(wù)需求:
加載外部配置文件,部署時更改比較方便。
先上代碼:
@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);
}
}
也可以通過@Value("${key}")使用
這中加載方法優(yōu)先級很高,如果與spring boot配置文件同名,將覆蓋application.properties文件中的配置。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
Spring+MyBatis多數(shù)據(jù)源配置實現(xiàn)示例
本篇文章主要介紹了Spring+MyBatis多數(shù)據(jù)源配置實現(xiàn)示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01
Spring boot搭建web應(yīng)用集成thymeleaf模板實現(xiàn)登陸
這篇文章主要介紹了Spring boot搭建web應(yīng)用集成thymeleaf模板實現(xiàn)登陸,頁面使用bootstrap,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12
Java異常 Factory method''sqlSessionFactory''rew exception;este
這篇文章主要介紹了Java異常 Factory method ‘sqlSessionFactory‘ threw exception; nested exception is java.lang.NoSuchMethodError:,本文介紹了springboot 引入mybatis-plus后報錯的解決方案,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-07-07
springboot之端口設(shè)置和contextpath的配置方式
這篇文章主要介紹了springboot之端口設(shè)置和contextpath的配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01
MyBatis解決Update動態(tài)SQL逗號的問題
這篇文章主要介紹了MyBatis解決Update動態(tài)SQL逗號的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01

