springboot启动时替换配置参数

SpringBoot启动时配置参数替换

一.背景

  • SpringBoot项目启动的时候,在不使用配置中心等的前提下或者有公司强制使用指定的"密码箱"情况下,需要远程获取关键配置信息,比如数据库密码,则需要在项目启动前获取配置并且进行本地配置替换。

二.Demo实现

1.maven依赖

xml 复制代码
<dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>

2.application.yml

yml 复制代码
spring:
    datasource:
        type: com.alibaba.druid.pool.DruidDataSource
        druid:
            driver-class-name: com.mysql.cj.jdbc.Driver
            url: jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
            username: root
            password: root
            initial-size: 10
            max-active: 100
            min-idle: 10
            max-wait: 60000
            pool-prepared-statements: true
            max-pool-prepared-statement-per-connection-size: 20
            time-between-eviction-runs-millis: 60000
            min-evictable-idle-time-millis: 300000
            #Oracle需要打开注释
            #validation-query: SELECT 1 FROM DUAL
            test-while-idle: true
            test-on-borrow: false
            test-on-return: false
            stat-view-servlet:
                enabled: true
                url-pattern: /druid/*
                #login-username: admin
                #login-password: admin
            filter:
                stat:
                    log-slow-sql: true
                    slow-sql-millis: 1000
                    merge-sql: false
                wall:
                    config:
                        multi-statement-allow: true

3.代码实现

  • 实现SpringBoot的接口EnvironmentPostProcessor
java 复制代码
package com.zzc.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class TestConfigEnvironmentProcessor implements EnvironmentPostProcessor {

    private static final Logger log = LoggerFactory.getLogger(TestConfigEnvironmentProcessor.class);

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        String password = environment.getProperty("spring.datasource.druid.password");
        System.out.println("System TestConfigEnvironmentProcessor password: " + password);
        MutablePropertySources mutablePropertySources = environment.getPropertySources();
        //TODO 创建单例等http请求工具,获取密码箱等配置,替换本地关键的配置
        Map<String, Object> map = new HashMap<>();
        map.put("spring.datasource.druid.password", "123456");
        mutablePropertySources.addFirst(new MapPropertySource("test", map));//自定的配置名称和配置项
        password = environment.getProperty("spring.datasource.druid.password");
        System.out.println("System TestConfigEnvironmentProcessor password: " + password);
    }
}

4.spring.factories配置

  • 在src/main/resources目录下创建文件夹META-INF,再创建文件spring.factories文件,新增配置项
base 复制代码
org.springframework.boot.env.EnvironmentPostProcessor=\
com.zzc.config.TestConfigEnvironmentProcessor
相关推荐
程序员张31 小时前
SpringBoot计时一次请求耗时
java·spring boot·后端
麦兜*8 小时前
Spring Boot启动优化7板斧(延迟初始化、组件扫描精准打击、JVM参数调优):砍掉70%启动时间的魔鬼实践
java·jvm·spring boot·后端·spring·spring cloud·系统架构
KK溜了溜了8 小时前
JAVA-springboot 整合Redis
java·spring boot·redis
大只鹅8 小时前
解决 Spring Boot 对 Elasticsearch 字段没有小驼峰映射的问题
spring boot·后端·elasticsearch
天河归来8 小时前
使用idea创建springboot单体项目
java·spring boot·intellij-idea
IT_10249 小时前
Spring Boot项目开发实战销售管理系统——数据库设计!
java·开发语言·数据库·spring boot·后端·oracle
武昌库里写JAVA9 小时前
Oracle如何使用序列 Oracle序列使用教程
java·开发语言·spring boot·学习·课程设计
超级小忍10 小时前
Spring Boot 中常用的工具类库及其使用示例(完整版)
spring boot·后端
程序员张310 小时前
SQL分析与打印-p6spy组件
spring boot·sql·mybatis·mybatisplus·p6spy
CHENWENFEIc11 小时前
SpringBoot论坛系统安全测试实战报告
spring boot·后端·程序人生·spring·系统安全·安全测试