參考資料
- Springboot中的@Profile注解
一. 使用場(chǎng)景
在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
但有時(shí)候,我們不通過配置文件,而是通過配置類的方式來指定不同環(huán)境下的配置信息,
此時(shí)就需要用到@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
注解分別作用于如下所示的兩個(gè)配置類上,分別指定dev
和product
環(huán)境下才能起作用。 - 我們通過
@Configuration
注解指定兩個(gè)配置類的Bean名稱
都是MyConfig,一般情況下會(huì)報(bào)錯(cuò),因?yàn)镾pring的IOC容器中,Bean的名稱是唯一的,但是我們使用了@Profile
注解指定了開發(fā)環(huán)境,不滿足指定開發(fā)環(huán)境的配置類不會(huì)被添加到Bean中,所以不會(huì)報(bào)錯(cuò)。
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 {
// 注入接口,會(huì)自動(dòng)從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)境文章來源:http://www.zghlxwxcb.cn/news/detail-491192.html
四. @Profile注解作用于方法上
4.1 定義一個(gè)生產(chǎn)環(huán)境的注解
- 當(dāng)
@Profile
注解作用于自定義注解上時(shí),自定義注解便可標(biāo)識(shí)開發(fā)環(huán)境,相當(dāng)于是@Profile(“開發(fā)環(huán)境名稱”)的簡(jiǎ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)境時(shí),才會(huì)注入IOC容器
@Bean
@Profile("dev")
public String getNameDev() {
return "devName";
}
// 生產(chǎn)環(huán)境時(shí),才會(huì)注入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)境
文章來源地址http://www.zghlxwxcb.cn/news/detail-491192.html
到了這里,關(guān)于SpringBoot 通過@Profile注解配置多環(huán)境的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!