什么网站有题目做,建设网站怎么判断是电脑还是手机,wordpress 您没有足够的权限访问该页面.,百度关键词查询工具SpringBoot启动时配置参数替换
一.背景
SpringBoot项目启动的时候#xff0c;在不使用配置中心等的前提下或者有公司强制使用指定的“密码箱”情况下#xff0c;需要远程获取关键配置信息#xff0c;比如数据库密码#xff0c;则需要在项目启动前获取配置并且进行本地配置…SpringBoot启动时配置参数替换
一.背景
SpringBoot项目启动的时候在不使用配置中心等的前提下或者有公司强制使用指定的“密码箱”情况下需要远程获取关键配置信息比如数据库密码则需要在项目启动前获取配置并且进行本地配置替换。
二.Demo实现
1.maven依赖
dependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactIdexclusionsexclusiongroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-logging/artifactId/exclusion/exclusions/dependencydependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactId/dependencydependencygroupIdorg.mybatis.spring.boot/groupIdartifactIdmybatis-spring-boot-starter/artifactId/dependencydependencygroupIdcom.alibaba/groupIdartifactIddruid-spring-boot-starter/artifactId/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdversion${lombok.version}/versionscopeprovided/scope/dependency/dependencies2.application.yml
spring:datasource:type: com.alibaba.druid.pool.DruidDataSourcedruid:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/test1?useUnicodetruecharacterEncodingUTF-8serverTimezoneAsia/Shanghaiusername: rootpassword: rootinitial-size: 10max-active: 100min-idle: 10max-wait: 60000pool-prepared-statements: truemax-pool-prepared-statement-per-connection-size: 20time-between-eviction-runs-millis: 60000min-evictable-idle-time-millis: 300000#Oracle需要打开注释#validation-query: SELECT 1 FROM DUALtest-while-idle: truetest-on-borrow: falsetest-on-return: falsestat-view-servlet:enabled: trueurl-pattern: /druid/*#login-username: admin#login-password: adminfilter:stat:log-slow-sql: trueslow-sql-millis: 1000merge-sql: falsewall:config:multi-statement-allow: true
3.代码实现
实现SpringBoot的接口EnvironmentPostProcessor
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);Overridepublic 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请求工具获取密码箱等配置替换本地关键的配置MapString, 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文件新增配置项
org.springframework.boot.env.EnvironmentPostProcessor\
com.zzc.config.TestConfigEnvironmentProcessor