SpringBoot项目启动的时候,在不使用配置中心等的前提下或者有公司强制使用指定的“密码箱”情况下,需要远程获取关键配置信息,比如数据库密码,则需要在项目启动前获取配置并且进行本地配置替换。 < dependencies> < dependency> < groupId> </ groupId> < artifactId> </ artifactId> < exclusions> < exclusion> < groupId> </ groupId> < artifactId> </ artifactId> </ exclusion> </ exclusions> </ dependency> < dependency> < groupId> </ groupId> < artifactId> </ artifactId> </ dependency> < dependency> < groupId> </ groupId> < artifactId> </ artifactId> </ dependency> < dependency> < groupId> </ groupId> < artifactId> </ artifactId> </ dependency> < dependency> < groupId> </ groupId> < artifactId> </ artifactId> < version> </ version> < scope> </ scope> </ dependency> </ dependencies> spring : datasource : type :  com.alibaba.druid.pool.DruidDataSourcedruid : driver-class-name :  com.mysql.cj.jdbc.Driverurl :  jdbc: mysql: //localhost: 3306/test1? useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai username :  rootpassword :  rootinitial-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 test-while-idle :  true test-on-borrow :  false test-on-return :  false stat-view-servlet : enabled :  true url-pattern :  /druid/*filter : stat : log-slow-sql :  true slow-sql-millis :  1000 merge-sql :  false wall : config : multi-statement-allow :  true 实现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 ) ; @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 ( ) ; Map < String ,  Object > =  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) ; } 
} 
在src/main/resources目录下创建文件夹META-INF,再创建文件spring.factories文件,新增配置项 org.springframework.boot.env.EnvironmentPostProcessor=\
com.zzc.config.TestConfigEnvironmentProcessor