通過實(shí)例了解Spring中@Profile的作用
這篇文章主要介紹了通過實(shí)例了解Spring中@Profile的作用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
根據(jù)系統(tǒng)環(huán)境的不同,Profile可以用來切換數(shù)據(jù)源。例如切換開發(fā),測(cè)試,生產(chǎn)環(huán)境的數(shù)據(jù)源。
舉個(gè)例子:
先創(chuàng)建配置類MainProfileConfig:
@Configuration
@PropertySource("classpath:/jdbc.properties")
public class MainProfileConfig implements EmbeddedValueResolverAware {
@Value("${db.user}")
private String user;
private String driverClass;
private StringValueResolver stringValueResolver;
@Profile("test")
@Bean("testDataSource")
public DataSource getTestDataSource(@Value("${db.password}") String pwd) throws PropertyVetoException {
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
comboPooledDataSource.setUser(user);
comboPooledDataSource.setPassword(pwd);
comboPooledDataSource.setDriverClass(driverClass);
return comboPooledDataSource;
}
@Profile("dev")
@Bean("devDataSource")
public DataSource getDevDataSource(@Value("${db.password}") String pwd) throws PropertyVetoException {
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
comboPooledDataSource.setUser(user);
comboPooledDataSource.setPassword(pwd);
comboPooledDataSource.setDriverClass(driverClass);
return comboPooledDataSource;
}
@Profile("pro")
@Bean("proDataSource")
public DataSource getproDataSource(@Value("${db.password}") String pwd) throws PropertyVetoException {
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
comboPooledDataSource.setUser(user);
comboPooledDataSource.setPassword(pwd);
comboPooledDataSource.setDriverClass(driverClass);
return comboPooledDataSource;
}
@Override
public void setEmbeddedValueResolver(StringValueResolver stringValueResolver) {
this.stringValueResolver = stringValueResolver;
driverClass = stringValueResolver.resolveStringValue("${db.driverClass}");
}
}
這里使用@Value和StringValueResolver來給屬性賦值
測(cè)試:運(yùn)行的時(shí)候設(shè)置環(huán)境 -Dspring.profiles.active=dev

public class ProFileTest {
@Test
public void test(){
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainProfileConfig.class);
String[] beanNamesForType = applicationContext.getBeanNamesForType(DataSource.class);
for (String name : beanNamesForType){
System.out.println(name);
}
applicationContext.close();
}
}
打印輸出:
devDataSource
也可以使用代碼的方式設(shè)置系統(tǒng)環(huán)境,創(chuàng)建容器的時(shí)候使用無參構(gòu)造方法,然后設(shè)置屬性。
@Test
public void test(){
//創(chuàng)建容器
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
//設(shè)置需要激活的環(huán)境
applicationContext.getEnvironment().setActiveProfiles("test");
//設(shè)置主配置類
applicationContext.register(MainProfileConfig.class);
//啟動(dòng)刷新容器
applicationContext.refresh();
String[] beanNamesForType = applicationContext.getBeanNamesForType(DataSource.class);
for (String name : beanNamesForType){
System.out.println(name);
}
applicationContext.close();
}
打印輸出:
testDataSource
setActiveProfiles設(shè)置環(huán)境的時(shí)候可以傳入多個(gè)值,它的方法可以接受多個(gè)參數(shù)。
public interface ConfigurableEnvironment extends Environment, ConfigurablePropertyResolver {
void setActiveProfiles(String... var1);
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot中實(shí)現(xiàn)數(shù)據(jù)脫敏處理的方法詳解
項(xiàng)目開發(fā)中,在處理敏感信息時(shí),數(shù)據(jù)脫敏是一項(xiàng)重要的安全措施,本文主要為大家介紹了如何在SpringBoot項(xiàng)目中進(jìn)行數(shù)據(jù)脫敏處理,有需要的可以了解下2025-03-03
解決springboot錯(cuò)誤:找不到或無法加載主類(配置編碼或者M(jìn)aven)
這篇文章主要介紹了解決springboot錯(cuò)誤:找不到或無法加載主類(配置編碼或者M(jìn)aven)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
IDEA連接MySQL后管理數(shù)據(jù)庫的操作指南
本節(jié)就來教大家如何在IDEA連接MySQL后管理數(shù)據(jù)庫(創(chuàng)建/修改/刪除數(shù)據(jù)庫、創(chuàng)建/修改/刪除表、插入/更新/刪除/查詢表記錄),文中通過圖文結(jié)合的方式給大家講解的非常詳細(xì),需要的朋友可以參考下2024-05-05
Java使用強(qiáng)大的Elastisearch搜索引擎實(shí)例代碼
本篇文章主要介紹了Java使用強(qiáng)大的Elastisearch搜索引擎實(shí)例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-05-05
Java中的 BigDecimal 和 String 的相互轉(zhuǎn)換問題
這篇文章主要介紹了Java中的 BigDecimal 和 String 的相互轉(zhuǎn)換問題,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05

