欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 八卦 > 修改springboot的配置文件

修改springboot的配置文件

2025/2/25 8:25:43 来源:https://blog.csdn.net/qq398581645/article/details/144566785  浏览:    关键词:修改springboot的配置文件

1 创建一个类实现EnvironmentPostProcessor

2 resources/META-INFO/spring.factories文件,配置实现类的全路径

以下是代码

实现类

package com.haier.configure.config;import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.boot.env.OriginTrackedMapPropertySource;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;import java.util.HashMap;
import java.util.Map;/*** @author A200* @date 2024/12/18 16:58*/
public class MyEnvPostProcessor implements EnvironmentPostProcessor {@Overridepublic void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {System.out.println("开始打印....");Map<String, Object> bootstrapSource = new HashMap<>();Map<String, Object> applicationSource = new HashMap<>();MutablePropertySources propertySources = environment.getPropertySources();for (PropertySource<?> propertySource : propertySources) {String propertySourceName = propertySource.getName();System.out.println("名称:"+propertySourceName);if (StringUtils.containsIgnoreCase(propertySourceName, "bootstrap.yml")) {System.out.println("bootstrap配置文件打印:");Map<String,Object> map = (Map<String,Object>)propertySource.getSource();bootstrapSource.putAll(map);bootstrapSource.put("wei.age","18");propertySources.remove(propertySourceName);propertySources.addLast(new OriginTrackedMapPropertySource(propertySourceName, bootstrapSource));continue;}if (StringUtils.containsIgnoreCase(propertySourceName, "application.yml")) {System.out.println("application配置文件打印:");Map<String,Object> map = (Map<String,Object>)propertySource.getSource();applicationSource.putAll(map);applicationSource.put("wei.name","longlonglong");applicationSource.put("wei.flag","true");propertySources.remove(propertySourceName);propertySources.addLast(new OriginTrackedMapPropertySource(propertySourceName, applicationSource));}}}
}

spring.factories文件

org.springframework.boot.env.EnvironmentPostProcessor=\com.haier.configure.config.BootstrapDecryptEnvironmentPostProcessor,\com.haier.configure.config.SystemDecryptEnvironmentPostProcessor,\com.haier.configure.config.MyEnvPostProcessor

测试controller

@Value("${wei.age}")
private Integer weiage;
@Value("${wei.name}")
private String weiname;
@Value("${wei.flag}")
private Boolean weiflag;@PostConstruct
public void init(){System.out.println("打印配置哦:"+weiage);System.out.println("打印配置哦:"+weiname);System.out.println("打印配置哦:"+weiflag);
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词