修改springboot的配置文件

1 创建一个类实现EnvironmentPostProcessor

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

以下是代码

实现类

java 复制代码
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 {

    @Override
    public 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文件

java 复制代码
org.springframework.boot.env.EnvironmentPostProcessor=\
  com.haier.configure.config.BootstrapDecryptEnvironmentPostProcessor,\
  com.haier.configure.config.SystemDecryptEnvironmentPostProcessor,\
  com.haier.configure.config.MyEnvPostProcessor

测试controller

java 复制代码
@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);
}
相关推荐
知识汲取者19 分钟前
巨量引擎 Marketing API Java SDK 介绍
java·开发语言
182******208322 分钟前
2026年40岁自学java还能找到工作吗
java·开发语言
yuzhiboyouye39 分钟前
java线程池
java·开发语言·firefox
网络工程小王1 小时前
【LCEL 链式调用详解】调用篇-2
java·服务器·前端·数据库·人工智能
Zzzzmo_1 小时前
【JavaEE】文件操作和IO
java·java-ee·io·文件操作·file·流对象
NE_STOP2 小时前
Redis-持久化之RDB
java
苍煜2 小时前
SpringBoot AOP切面编程精讲:实现方式、Spring区别及与自定义注解生产实战
java·spring boot·spring
胡小禾2 小时前
企业内部文件处理方案
java
常利兵3 小时前
Spring Boot:别再重复造轮子,这些内置功能香麻了
java·spring boot·后端
咸鱼翻身小阿橙3 小时前
Qt QML调用C++注册类
java·c++·qt