最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Springboo如何t動態(tài)修改配置文件屬性

 更新時間:2023年09月16日 15:04:18   作者:kenick  
這篇文章主要介紹了Springboo如何t動態(tài)修改配置文件屬性問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

Springboot動態(tài)修改配置文件屬性

package com.kenick.config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class CustomApplicationProperties implements EnvironmentPostProcessor {
    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        MutablePropertySources propertySources = environment.getPropertySources();
        // 所有屬性文件名
        String env = "";
        Iterator<PropertySource<?>> iterator = propertySources.iterator();
        System.out.println(" ================== 所有配置文件名 ==================");
        while(iterator.hasNext()){
            PropertySource<?> next = iterator.next();
            String configFileName = next.getName();
            System.out.println(configFileName);
            if(configFileName.contains("application-")){ // spring只會加載當(dāng)前環(huán)境配置
                env = configFileName.split("application-")[1].replace(".properties]", "");
            }
        }
        // 獲取主配置文件
        String mainName = "applicationConfig: [classpath:/application.properties]";
        MapPropertySource propertySource = (MapPropertySource) propertySources.get(mainName);
        Map<String, Object> source = propertySource.getSource();
        System.out.println(" ================== 主配置 ==================");
        source.forEach((k,v) -> {
            System.out.println(k+":"+v.toString());
        });
        // 獲取激活配置文件
        System.out.println("當(dāng)前環(huán)境:" + env);
        String activeName = "applicationConfig: [classpath:/application-" + env + ".properties]";
        MapPropertySource activePropertySource = (MapPropertySource) propertySources.get(activeName);
        Map<String, Object> activeSource = activePropertySource.getSource();
        System.out.println(" ================== 當(dāng)前配置 ==================");
        Map<String, Object> newConfigMap = new HashMap<>();
        activeSource.forEach((k,v) -> {
            System.out.println(k+":"+v.toString());
            newConfigMap.put(k, v.toString()); // value必須要放入String格式
        });
        // 可動態(tài)修改配置
        // newConfigMap.replace("log.custom.test", "E:\\tmp\\logs");
        propertySources.replace(activeName, new MapPropertySource(activeName , newConfigMap));
    }
}

org.springframework.boot.env.EnvironmentPostProcessor=com.kenick.config.CustomApplicationProperties

動態(tài)給springBoot配置增加屬性

有些時候我們要把代碼部署到不同地方,根據(jù)地方不同來辨別要執(zhí)行那些代碼,這時就要把一些配置事先配置到數(shù)據(jù)庫在在容器初始化時讀取對應(yīng)數(shù)據(jù)來執(zhí)行特定需求。

好處可以把一些配置文件配置到數(shù)據(jù)庫。用到@PostConstruct注解與@DependesOn注解

@PostConstruct注解

配置了此注解方法會在調(diào)用構(gòu)造方法后自動被調(diào)用,也可以理解為spring容器初始化的時候執(zhí)行該方法。

實例代碼如下

  @Component
  public class Config3 {
      @Autowired
      private ConfigurableEnvironment environment;
      @PostConstruct
      public void setProvinceCode() {
          MutablePropertySources propertySources = environment.getPropertySources();
          Pattern p = Pattern.compile("^applicationConfig.*");
          for (PropertySource<?> propertySource : propertySources) {
              if (p.matcher(propertySource.getName()).matches()) {
                  Properties properties = new Properties();
                  Map<String, String> map = new HashMap<>();
                  map.put("code","10000000000");
                  properties.putAll(map);
                  PropertiesPropertySource constants = new PropertiesPropertySource("system-config", properties);
                  propertySources.addAfter(propertySource.getName(),constants);
              }
          }
      }
  }

@DependesOn注解

用來表示一個Bean的實例化依賴另一個Bean的實例化

  @DependsOn("config3")
  @Component
  public class Config2 {
      @Value("$[code]")
      private String codeValue;
      public void show() {
          System.out.println(codeValue);
      }
      @Value("$[code]")
      public void setProvinceCode(String codeValue) {
          System.out.println("code:"+codeValue);
      }
  }

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 使用Python高效實現(xiàn)刪除PDF中的超鏈接

    使用Python高效實現(xiàn)刪除PDF中的超鏈接

    在數(shù)字文檔時代,PDF文件因其跨平臺兼容性和版式固定性,成為信息交換的常用載體,本文將引導(dǎo)你使用一個高效的Python庫,輕松實現(xiàn)PDF超鏈接的批量刪除,感興趣的小伙伴可以了解下
    2025-10-10
  • 我用Python抓取了7000 多本電子書案例詳解

    我用Python抓取了7000 多本電子書案例詳解

    這篇文章主要介紹了我用Python抓取了7000 多本電子書案例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • 從基礎(chǔ)到高級詳解Python對象序列化的實戰(zhàn)指南

    從基礎(chǔ)到高級詳解Python對象序列化的實戰(zhàn)指南

    在軟件開發(fā)中,對象序列化是一項至關(guān)重要的技術(shù),它允許將內(nèi)存中的復(fù)雜對象轉(zhuǎn)換為可以存儲或傳輸?shù)母袷?本文將展示如何在不同場景下選擇和應(yīng)用最合適的序列化方案,下面就跟隨小編一起了解下吧
    2025-09-09
  • Python之split函數(shù)的深入理解

    Python之split函數(shù)的深入理解

    split函數(shù)主要應(yīng)用場景是Python對字符串的處理中(數(shù)據(jù)分析,數(shù)據(jù)處理),以及計算機(jī)二級考試的??蓟A(chǔ)知識點,這篇文章主要介紹了Python之split函數(shù)的詳解,需要的朋友可以參考下
    2023-02-02
  • pd.drop_duplicates刪除重復(fù)行的方法實現(xiàn)

    pd.drop_duplicates刪除重復(fù)行的方法實現(xiàn)

    drop_duplicates 方法實現(xiàn)對數(shù)據(jù)框 DataFrame 去除特定列的重復(fù)行,本文主要介紹了pd.drop_duplicates刪除重復(fù)行的方法實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • Python實現(xiàn)簡單的代理服務(wù)器

    Python實現(xiàn)簡單的代理服務(wù)器

    這篇文章主要介紹了Python實現(xiàn)簡單的代理服務(wù)器,可實現(xiàn)代理服務(wù)器基本的包轉(zhuǎn)發(fā)功能,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-07-07
  • python 利用pywifi模塊實現(xiàn)連接網(wǎng)絡(luò)破解wifi密碼實時監(jiān)控網(wǎng)絡(luò)

    python 利用pywifi模塊實現(xiàn)連接網(wǎng)絡(luò)破解wifi密碼實時監(jiān)控網(wǎng)絡(luò)

    這篇文章主要介紹了python 利用pywifi模塊實現(xiàn)連接網(wǎng)絡(luò)破解wifi密碼實時監(jiān)控網(wǎng)絡(luò),需要的朋友可以參考下
    2019-09-09
  • Python sep參數(shù)使用方法詳解

    Python sep參數(shù)使用方法詳解

    這篇文章主要介紹了Python sep參數(shù)使用方法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-02-02
  • Python Datetime模塊和Calendar模塊用法實例分析

    Python Datetime模塊和Calendar模塊用法實例分析

    這篇文章主要介紹了Python Datetime模塊和Calendar模塊用法,結(jié)合實例形式分析了Python日期時間及日歷相關(guān)的Datetime模塊和Calendar模塊原理、用法及操作注意事項,需要的朋友可以參考下
    2019-04-04
  • Python保存環(huán)境方式(導(dǎo)出requirements.txt文件)

    Python保存環(huán)境方式(導(dǎo)出requirements.txt文件)

    這篇文章主要介紹了Python保存環(huán)境方式(導(dǎo)出requirements.txt文件),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-04-04

最新評論

江门市| 泊头市| 江津市| 武陟县| 华坪县| 富阳市| 广丰县| 诸城市| 珠海市| 枝江市| 东丰县| 德惠市| 开平市| 鹤峰县| 长乐市| 紫金县| 申扎县| 砀山县| 苏尼特右旗| 郧西县| 西华县| 阿克苏市| 东海县| 延安市| 璧山县| 三河市| 阿城市| 常熟市| 观塘区| 清苑县| 建德市| 嘉祥县| 大余县| 新乡市| 恭城| 石狮市| 盐源县| 化州市| 陵川县| 临西县| 绍兴县|