SpringBoot如何通過@Profile注解配置多環(huán)境
一. 使用場景
在Spring中,可以使用配置文件的方式來指定不同環(huán)境下所需要的配置信息
?application.yml
spring:
profiles:
# 通過active來指定當(dāng)前所處的開發(fā)環(huán)境
active: dev?application-dev.yml
spring:
datasource:
url: jdbc:mysql://localhost/myblog-dev
username: dev
password: mysql
driver-class-name: com.mysql.cj.jdbc.Driver?application-product.yml
spring:
datasource:
url: jdbc:mysql://localhost/myblog-product
username: product
password: mysql
driver-class-name: com.mysql.cj.jdbc.Driver但有時候,我們不通過配置文件,而是通過配置類的方式來指定不同環(huán)境下的配置信息,
此時就需要用到@Profile注解。
二. 前期準(zhǔn)備
?用來封裝數(shù)據(jù)庫信息的Entity
import lombok.Builder;
import lombok.Data;
@Builder
@Data
public class DBInfoEntity {
private String url;
private String port;
private String userName;
private String password;
}?配置接口
public interface Config {
// 獲取數(shù)據(jù)庫信息
DBInfoEntity getDBInfo();
// 獲取系統(tǒng)URL
String getSystemUrl();
}三. @Profile注解作用于類上
- 我們使用
@Profile注解分別作用于如下所示的兩個配置類上,分別指定dev和product環(huán)境下才能起作用。 - 我們通過
@Configuration注解指定兩個配置類的Bean名稱都是MyConfig,一般情況下會報(bào)錯,因?yàn)镾pring的IOC容器中,Bean的名稱是唯一的,但是我們使用了@Profile注解指定了開發(fā)環(huán)境,不滿足指定開發(fā)環(huán)境的配置類不會被添加到Bean中,所以不會報(bào)錯。
3.1 配置類
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration("MyConfig")
// 指定開發(fā)環(huán)境為dev
@Profile("dev")
public class MyConfig1 implements Config {
@Override
public DBInfoEntity getDBInfo() {
return DBInfoEntity.builder()
.url("https://127.0.0.1")
.port("8080")
.userName("devUser")
.password("110120")
.build();
}
@Override
public String getSystemUrl() {
return "https://www.dev.com";
}
}import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration("MyConfig")
// 指定開發(fā)環(huán)境為product
@Profile("product")
public class MyConfig2 implements Config {
@Override
public DBInfoEntity getDBInfo() {
return DBInfoEntity.builder()
.url("https://127.0.0.2")
.port("8089")
.userName("prodUser")
.password("999000")
.build();
}
@Override
public String getSystemUrl() {
return "https://www.prod.com";
}
}3.2 效果
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class Test32Controller implements CommandLineRunner {
// 注入接口,會自動從IOC容器中獲取該接口的實(shí)現(xiàn)類
@Autowired
private Config config;
@Override
public void run(String... args) throws Exception {
DBInfoEntity dbInfo = config.getDBInfo();
System.out.println(dbInfo);
String systemUrl = config.getSystemUrl();
System.out.println(systemUrl);
}
}??????dev環(huán)境

??????product環(huán)境

四. @Profile注解作用于方法上
4.1 定義一個生產(chǎn)環(huán)境的注解
當(dāng)@Profile注解作用于自定義注解上時,自定義注解便可標(biāo)識開發(fā)環(huán)境,相當(dāng)于是@Profile(“開發(fā)環(huán)境名稱”)的簡寫方式。
import org.springframework.context.annotation.Profile;
import java.lang.annotation.*;
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@Profile("product")
public @interface ProductionAnnotation {
}4.2 配置類
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration
public class MyConfig3 {
// 開發(fā)環(huán)境時,才會注入IOC容器
@Bean
@Profile("dev")
public String getNameDev() {
return "devName";
}
// 生產(chǎn)環(huán)境時,才會注入IOC容器
@Bean
@ProductionAnnotation // 相當(dāng)于 @Profile("product")
public String getNameProduct() {
return "productName";
}
}4.3 效果
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
@Component
public class Test32Controller implements CommandLineRunner {
@Autowired
private ApplicationContext applicationContext;
@Override
public void run(String... args) throws Exception {
// 判斷當(dāng)前IOC容器中是否存在名稱為 getNameDev 的Bean
boolean getNameDevExist = applicationContext.containsBean("getNameDev");
if (getNameDevExist) {
// 從IOC容器中獲取出名稱為 getNameDev 的Bean
Object bean1 = applicationContext.getBean("getNameDev");
System.out.println("目前所在的是開發(fā)環(huán)境!");
System.out.println(bean1);
}
boolean getNameProductExist = applicationContext.containsBean("getNameProduct");
if (getNameProductExist) {
Object bean2 = applicationContext.getBean("getNameProduct");
System.out.println("目前所在的是生產(chǎn)環(huán)境!");
System.out.println(bean2);
}
}
}??????dev環(huán)境

??????product環(huán)境

參考資料
到此這篇關(guān)于SpringBoot 通過@Profile注解配置多環(huán)境的文章就介紹到這了,更多相關(guān)SpringBoot 配置多環(huán)境內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MybatisPlusException:Failed?to?process,Error?SQL異常報(bào)錯的解決辦法
這篇文章主要給大家介紹了關(guān)于MybatisPlusException:Failed?to?process,Error?SQL異常報(bào)錯的解決辦法,文中通過實(shí)例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2023-03-03
Mybatis基于xml配置實(shí)現(xiàn)單表的增刪改查功能
這篇文章主要介紹了Mybatis基于xml配置實(shí)現(xiàn)單表的增刪改查,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04
Java后臺通過Collections獲取list集合中最大數(shù),最小數(shù)代碼
這篇文章主要介紹了Java后臺通過Collections獲取list集合中最大數(shù),最小數(shù)代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
IntelliJ IDEA創(chuàng)建maven多模塊項(xiàng)目(圖文教程)
這篇文章主要介紹了IntelliJ IDEA創(chuàng)建maven多模塊項(xiàng)目(圖文教程),非常具有實(shí)用價值,需要的朋友可以參考下2017-09-09
java+Okhttp3調(diào)用接口的實(shí)例
這篇文章主要介紹了java+Okhttp3調(diào)用接口的實(shí)例,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12

