亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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只會加載當前環(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("當前環(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(" ================== 當前配置 ==================");
        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ù)庫在在容器初始化時讀取對應數(shù)據(jù)來執(zhí)行特定需求。

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

@PostConstruct注解

配置了此注解方法會在調用構造方法后自動被調用,也可以理解為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);
      }
  }

總結

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

相關文章

  • Pycharm關閉控制臺多余窗口的解決辦法

    Pycharm關閉控制臺多余窗口的解決辦法

    這篇文章主要介紹了Pycharm關閉控制臺多余窗口的解決辦法,文中通過圖文結合的方式講解的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下
    2024-12-12
  • 利用Python為女神制作一個專屬網(wǎng)站

    利用Python為女神制作一個專屬網(wǎng)站

    520不知道送什么禮物?快跟隨小編一起學習一下如何利用Python語言制作一個專屬的網(wǎng)站送給女神吧!文中的示例代碼講解詳細,需要的可以參考一下
    2022-05-05
  • python讀取dicom圖像示例(SimpleITK和dicom包實現(xiàn))

    python讀取dicom圖像示例(SimpleITK和dicom包實現(xiàn))

    今天小編就為大家分享一篇python讀取dicom圖像示例(SimpleITK和dicom包實現(xiàn)),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • 如何更改 pandas dataframe 中兩列的位置

    如何更改 pandas dataframe 中兩列的位置

    如何更改 pandas dataframe 中兩列的位置?今天小編就為大家介紹兩種操作方法,希望對大家有所幫助,還等什么?一起跟隨小編過來看看吧
    2019-12-12
  • 利用Python和PyQt5構建一個多功能PDF轉換器

    利用Python和PyQt5構建一個多功能PDF轉換器

    在日常工作中,處理PDF文件幾乎是每個人都不可避免的任務,本文將通過Python和PyQt5搭建一個強大的PDF文件處理平臺,希望對大家有所幫助
    2024-12-12
  • 詳解Python中的Dict(下篇)

    詳解Python中的Dict(下篇)

    這篇文章主要為大家介紹了Python中的Dict,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-12-12
  • pytorch方法測試詳解——歸一化(BatchNorm2d)

    pytorch方法測試詳解——歸一化(BatchNorm2d)

    今天小編就為大家分享一篇pytorch方法測試詳解——歸一化(BatchNorm2d),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • Django ORM框架的定時任務如何使用詳解

    Django ORM框架的定時任務如何使用詳解

    這篇文章主要給大家介紹了關于Django ORM框架的定時任務如何使用的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用django具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。
    2017-10-10
  • python進行TCP端口掃描的實現(xiàn)

    python進行TCP端口掃描的實現(xiàn)

    這篇文章主要介紹了python進行TCP端口掃描的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12
  • django3.02模板中的超鏈接配置實例代碼

    django3.02模板中的超鏈接配置實例代碼

    在本篇文章里小編給大家整理了關于django3.02模板中的超鏈接配置實例代碼內(nèi)容,需要的朋友們可以學習參考下。
    2020-02-02

最新評論