通過實例了解Spring中@Profile的作用
這篇文章主要介紹了通過實例了解Spring中@Profile的作用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
根據(jù)系統(tǒng)環(huán)境的不同,Profile可以用來切換數(shù)據(jù)源。例如切換開發(fā),測試,生產(chǎn)環(huán)境的數(shù)據(jù)源。
舉個例子:
先創(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來給屬性賦值
測試:運行的時候設(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)建容器的時候使用無參構(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);
//啟動刷新容器
applicationContext.refresh();
String[] beanNamesForType = applicationContext.getBeanNamesForType(DataSource.class);
for (String name : beanNamesForType){
System.out.println(name);
}
applicationContext.close();
}
打印輸出:
testDataSource
setActiveProfiles設(shè)置環(huán)境的時候可以傳入多個值,它的方法可以接受多個參數(shù)。
public interface ConfigurableEnvironment extends Environment, ConfigurablePropertyResolver {
void setActiveProfiles(String... var1);
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot?No?bean?named?'XXXXX'?available?問
這篇文章主要介紹了Springboot?No?bean?named?'XXXXX'?available?問題解決方法,解決方法也很簡單,盡量規(guī)范類的命名,注解中指定bean名稱,本文給大家介紹的非常詳細,需要的朋友可以參考下2023-07-07
Springboot swagger配置過程詳解(idea社區(qū)版2023.1.4+apache-maven-3
這篇文章主要介紹了Springboot-swagger配置(idea社區(qū)版2023.1.4+apache-maven-3.9.3-bin),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07
SpringBoot2.1.x,創(chuàng)建自己的spring-boot-starter自動配置模塊操作
這篇文章主要介紹了SpringBoot2.1.x,創(chuàng)建自己的spring-boot-starter自動配置模塊操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
Spring?@Cacheable注解類內(nèi)部調(diào)用失效的解決方案
這篇文章主要介紹了Spring?@Cacheable注解類內(nèi)部調(diào)用失效的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01

