我們知道實(shí)際工作中一般都會(huì)有本地開(kāi)發(fā)環(huán)境(一般開(kāi)發(fā)環(huán)境都等同于dev環(huán)境)蓄氧,uat測(cè)試環(huán)境(test)弧满,生產(chǎn)(pro)環(huán)境汽煮,不同的環(huán)境都需要讀取不同環(huán)境的配置。本篇博客就討論實(shí)際工作中springboot多環(huán)境配置管理睡毒。
比如我不同的環(huán)境定義了不同環(huán)境的配置文件
開(kāi)發(fā)環(huán)境来惧,application.properties:
spring.redis.host=192.168.1.111
測(cè)試環(huán)境,application-test.properties:
spring.redis.host=172.168.1.121
生產(chǎn)環(huán)境演顾,application-pro.properties:
spring.redis.host=10.10.1.121
啟動(dòng)類:
package com.zhihao.miao;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
ConfigurableApplicationContext context =SpringApplication.run(Application.class,args);
String redishost = context.getEnvironment().getProperty("spring.redis.host");
System.out.println(redishost);
}
}
啟動(dòng)并打庸┎蟆:
我們發(fā)現(xiàn)默認(rèn)讀的配置文件是application.properties,那么我們要去讀取開(kāi)發(fā)或者生產(chǎn)環(huán)境的配置文件呢隅居?
有多種方式,
第一種葛虐,比如在application.properties中配置屬性:
spring.redis.host=192.168.1.111
spring.profiles.active=test
發(fā)現(xiàn)已經(jīng)讀取到測(cè)試環(huán)境的配置胎源。
第二種方式,啟動(dòng)項(xiàng)目的時(shí)候指定參數(shù)
idea配置頁(yè)面屿脐,exclipse同理涕蚤。
第三種,也可以在代碼里配置的诵,硬編碼(不推薦)
package com.zhihao.miao;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class Application2 {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(Application.class);
springApplication.setAdditionalProfiles("test");
ConfigurableApplicationContext context =springApplication.run(args);
String redishost =context.getEnvironment().getProperty("spring.redis.host");
System.out.println(redishost);
}
}
第四種打包環(huán)境万栅,打包時(shí)指定
java -jar springboot-profile2-1.0-SNAPSHOT.jar --spring.profiles.active=pro
當(dāng)然上面都可以指定多個(gè)profile文件,實(shí)際開(kāi)發(fā)中很少這么做西疤。
定義開(kāi)發(fā)的application.properties
spring.redis.host=192.168.1.111
spring.profiles.active=test,pro
測(cè)試環(huán)境的application-test.properties:
spring.redis.host=172.168.1.121
spring.redis.database=0
生產(chǎn)中的application-pro.properties
spring.redis.host=10.10.1.121
spring.redis.password=323dsds32e3
啟動(dòng)類:
package com.zhihao.miao;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class Application3 {
public static void main(String[] args) {
ConfigurableApplicationContext context =SpringApplication.run(Application3.class,args);
String redishost = context.getEnvironment().getProperty("spring.redis.host");
String redisdatabase = context.getEnvironment().getProperty("spring.redis.database");
String redispassword = context.getEnvironment().getProperty("spring.redis.password");
System.out.println(redishost);
System.out.println(redisdatabase);
System.out.println(redispassword);
}
}
發(fā)現(xiàn)即讀到測(cè)試的配置又讀到生產(chǎn)的配置烦粒,測(cè)試,生產(chǎn)都有的配置取后配置的代赁,比如我這邊配置的
spring.profiles.active=test,pro
測(cè)試扰她,生產(chǎn)都有spring.redis.host的配置,但是系統(tǒng)讀到的是生產(chǎn)的配置芭碍。
根據(jù)不同的環(huán)境初始化不同的bean
定義一個(gè)普通的配置類MyConfig徒役,代碼如下:
package com.zhihao.miao.bean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyConfig {
@Bean
public Runnable createDevRunnable(){
System.out.println("========dev========");
return () -> {};
}
@Bean
public Runnable createTestRunnable(){
System.out.println("========test========");
return () -> {};
}
@Bean
public Runnable createProRunnable(){
System.out.println("========pro========");
return () -> {};
}
}
啟動(dòng)類啟動(dòng)執(zhí)行:
package com.zhihao.miao;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class Application4 {
public static void main(String[] args) {
ConfigurableApplicationContext context =SpringApplication.run(Application4.class,args);
System.out.println(context.getBeansOfType(Runnable.class));
}
}
發(fā)現(xiàn)三個(gè)類都初始化了,
需求這樣窖壕,不同的環(huán)境初始化不同環(huán)境的bean忧勿,使用 @Profile注解,
配置類修改如下:
package com.zhihao.miao.bean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration
public class MyConfig {
@Bean
@Profile("dev")
public Runnable createDevRunnable(){
System.out.println("========dev========");
return () -> {};
}
@Bean
@Profile("test")
public Runnable createTestRunnable(){
System.out.println("========test========");
return () -> {};
}
@Bean
@Profile("pro")
public Runnable createProRunnable(){
System.out.println("========pro========");
return () -> {};
}
}
指定測(cè)試環(huán)境艇拍,啟動(dòng)啟動(dòng)類狐蜕,發(fā)現(xiàn)
發(fā)現(xiàn)只初始化了測(cè)試環(huán)境的bean宠纯。
注意:@Profile也可以修飾類卸夕,和修飾方法一致,這邊就不多做過(guò)多解釋了婆瓜。