Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏

5.5. 命令行注入配置项

5.5.1. spring.profiles.active 参数切换配置文件

首先我们准备三个配置文件

			
src/main/resource/application-development.properties
src/main/resource/application-testing.properties
src/main/resource/application-production.properties			
			
			

使用下面--spring.profiles.active参数切换运行环境配置文件

			
java -jar application.jar --spring.profiles.active=development
java -jar application.jar --spring.profiles.active=testing
java -jar application.jar --spring.profiles.active=production			
			
			

分别为三个环境打包

			
mvn clean package -Pdevelopment
mvn clean package -Ptesting
mvn clean package -Pproduction			
			
			

5.5.2. SpringApplicationBuilder.properties() 方法添加配置项

			
			
  public static void main(String[] args) {
    new SpringApplicationBuilder(Application.class).properties("spring.config.name=client").run(args);
  }			
			
			

5.5.3. 禁用命令行注入环境变量

		
SpringApplication.setAddCommandLineProperties(false)