ToB企服应用市场:ToB评测及商务社交产业平台

标题: 修改springboot的配置文件 [打印本页]

作者: 河曲智叟    时间: 2024-12-19 22:40
标题: 修改springboot的配置文件
1 创建一个类实现EnvironmentPostProcessor
2 resources/META-INFO/spring.factories文件,配置实现类的全路径
以下是代码
实现类
  1. package com.haier.configure.config;
  2. import org.apache.commons.lang3.StringUtils;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.env.EnvironmentPostProcessor;
  5. import org.springframework.boot.env.OriginTrackedMapPropertySource;
  6. import org.springframework.core.env.ConfigurableEnvironment;
  7. import org.springframework.core.env.MutablePropertySources;
  8. import org.springframework.core.env.PropertySource;
  9. import java.util.HashMap;
  10. import java.util.Map;
  11. /**
  12. * @author A200
  13. * @date 2024/12/18 16:58
  14. */
  15. public class MyEnvPostProcessor implements EnvironmentPostProcessor {
  16.     @Override
  17.     public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
  18.         System.out.println("开始打印....");
  19.         Map<String, Object> bootstrapSource = new HashMap<>();
  20.         Map<String, Object> applicationSource = new HashMap<>();
  21.         MutablePropertySources propertySources = environment.getPropertySources();
  22.         for (PropertySource<?> propertySource : propertySources) {
  23.             String propertySourceName = propertySource.getName();
  24.             System.out.println("名称:"+propertySourceName);
  25.             if (StringUtils.containsIgnoreCase(propertySourceName, "bootstrap.yml")) {
  26.                 System.out.println("bootstrap配置文件打印:");
  27.                 Map<String,Object> map = (Map<String,Object>)propertySource.getSource();
  28.                 bootstrapSource.putAll(map);
  29.                 bootstrapSource.put("wei.age","18");
  30.                 propertySources.remove(propertySourceName);
  31.                 propertySources.addLast(new OriginTrackedMapPropertySource(propertySourceName, bootstrapSource));
  32.                 continue;
  33.             }
  34.             if (StringUtils.containsIgnoreCase(propertySourceName, "application.yml")) {
  35.                 System.out.println("application配置文件打印:");
  36.                 Map<String,Object> map = (Map<String,Object>)propertySource.getSource();
  37.                 applicationSource.putAll(map);
  38.                 applicationSource.put("wei.name","longlonglong");
  39.                 applicationSource.put("wei.flag","true");
  40.                 propertySources.remove(propertySourceName);
  41.                 propertySources.addLast(new OriginTrackedMapPropertySource(propertySourceName, applicationSource));
  42.             }
  43.         }
  44.     }
  45. }
复制代码
spring.factories文件
  1. org.springframework.boot.env.EnvironmentPostProcessor=\
  2.   com.haier.configure.config.BootstrapDecryptEnvironmentPostProcessor,\
  3.   com.haier.configure.config.SystemDecryptEnvironmentPostProcessor,\
  4.   com.haier.configure.config.MyEnvPostProcessor
复制代码

测试controller
  1. @Value("${wei.age}")
  2. private Integer weiage;
  3. @Value("${wei.name}")
  4. private String weiname;
  5. @Value("${wei.flag}")
  6. private Boolean weiflag;
  7. @PostConstruct
  8. public void init(){
  9.     System.out.println("打印配置哦:"+weiage);
  10.     System.out.println("打印配置哦:"+weiname);
  11.     System.out.println("打印配置哦:"+weiflag);
  12. }
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4