修改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);
}
相关推荐
Joy T2 小时前
Agent 确定性上下文设计:从 Prompt 参数注入到能力分层
java·开发语言·微服务·架构·prompt·agent context
m0_587383005 小时前
广州 24 小时自助健身房解决方案技术实现与实战指南
java·spring·小程序·架构·需求分析
·云扬·5 小时前
Prompt 工程与安全:从 System 设计到注入防御
java·人工智能·安全·ai·prompt
代码不停6 小时前
自动化测试(java)
java·开发语言·selenium·自动化
唠点键盘之外的7 小时前
26 向量数据库实战:从 0 搭一条能用的检索链路(pgvector + Spring AI)
数据库·人工智能·spring
鬼手点金8 小时前
opencode-rules 插件
java·开发语言·前端·人工智能·chrome·git·github
paopaokaka_luck9 小时前
滑雪场管理系统(AI智能助手流式对话,ECharts 运营数据分析、教练、雪具与场地一体化预约,钱包支付退款、订单核销与服务评价)
java·人工智能·spring boot·网络协议·数据分析·echarts
SL_staff9 小时前
JVS混合架构实践:能力引擎+动态模型如何支撑企业级可持续交付
spring boot·低代码·全栈
迪飞特科技9 小时前
智慧物业平台实战:Spring Boot 整合小程序实现报修巡检费用全流程
spring boot·后端·小程序
考虑考虑9 小时前
@JsonInclude注解使用
spring boot·后端·spring