springboot修改配置文件的value

/*
 * Copyright (c) Honor Device Co., Ltd. 2022-2022. All rights reserved.
 */

package com.hihonor.it.pur.supinfo.interfaces.facade.controller;

import java.util.HashMap;
import java.util.Map;

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 org.springframework.stereotype.Component;

@Component
public class DecodeEnvironmentPostProcessor implements EnvironmentPostProcessor {
    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        String env = environment.getProperty("spring.profiles.active");
        MutablePropertySources sources = environment.getPropertySources();
        PropertySource<?> applicationProperty = sources.get("applicationConfig: [classpath:/application.properties]");
        Object source = applicationProperty.getSource();
        System.out.println(source);
        Map map = new HashMap<>();
        map.put("datasource.password.1", "fewgewgew");
        MapPropertySource newSource = new MapPropertySource("application-decode", map);
        environment.getPropertySources().addBefore(applicationProperty.getName(), newSource);
        System.out.println(newSource);
        System.out.println("pwd: " + environment.getProperty("datasource.password.1"));
    }
}

在class目录下必须要有约定的文件配置META-INF/spring.factories

org.springframework.boot.env.EnvironmentPostProcessor=com.test.it.interfaces.facade.controller.DecodeEnvironmentPostProcessor

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值