欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

spring boot啟動(dòng)時(shí)加載外部配置文件的方法

 更新時(shí)間:2018年02月09日 08:37:32   作者:workabee  
這篇文章主要給大家介紹了關(guān)于spring boot啟動(dòng)時(shí)加載外部配置文件的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。

前言

相信很多人選擇Spring Boot主要是考慮到它既能兼顧Spring的強(qiáng)大功能,還能實(shí)現(xiàn)快速開(kāi)發(fā)的便捷。本文主要給大家介紹了關(guān)于spring boot啟動(dòng)時(shí)加載外部配置文件的相關(guān)內(nèi)容,下面話(huà)不多說(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();
 }
}

使用變量的工具類(lèi)

@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ì)腳本之家的支持。

相關(guān)文章

  • Springboot使用test無(wú)法啟動(dòng)問(wè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)示例

    本篇文章主要介紹了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)登陸

    這篇文章主要介紹了Spring boot搭建web應(yīng)用集成thymeleaf模板實(shí)現(xiàn)登陸,頁(yè)面使用bootstrap,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • 歸并算法之有序數(shù)組合并算法實(shí)現(xiàn)

    歸并算法之有序數(shù)組合并算法實(shí)現(xiàn)

    這篇文章主要介紹了歸并算法之有序數(shù)組合并算法實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下
    2017-07-07
  • Spring中屬性注入詳解

    Spring中屬性注入詳解

    這篇文章主要為大家詳細(xì)介紹了Spring中屬性注入,演示了int、String、數(shù)組、list等屬性的注入,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • Java異常 Factory method''sqlSessionFactory''rew exception;ested exception is java.lang.NoSuchMethodError:

    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
  • java壓縮文件與刪除文件的示例代碼

    java壓縮文件與刪除文件的示例代碼

    這篇文章主要介紹了java壓縮文件與刪除文件的示例代碼,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-08-08
  • 全網(wǎng)最全Mybatis-Plus詳解

    全網(wǎng)最全Mybatis-Plus詳解

    Mybatis-Plus是一個(gè)Mybatis(opens?new?window)的增強(qiáng)工具,在Mybatis的基礎(chǔ)上只做增強(qiáng)不做改變,為簡(jiǎn)化開(kāi)發(fā),這篇文章主要介紹了全網(wǎng)最全Mybatis-Plus詳解,需要的朋友可以參考下
    2024-05-05
  • springboot之端口設(shè)置和contextpath的配置方式

    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)題

    這篇文章主要介紹了MyBatis解決Update動(dòng)態(tài)SQL逗號(hào)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01

最新評(píng)論