這篇文章主要介紹了通過實例了解Spring中@Profile的作用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
根據系統環境的不同,Profile可以用來切換數據源。例如切換開發,測試,生產環境的數據源。
舉個例子:
先創建配置類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來給屬性賦值
測試:運行的時候設置環境 -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
也可以使用代碼的方式設置系統環境,創建容器的時候使用無參構造方法,然后設置屬性。
@Test
public void test(){
//創建容器
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
//設置需要激活的環境
applicationContext.getEnvironment().setActiveProfiles("test");
//設置主配置類
applicationContext.register(MainProfileConfig.class);
//啟動刷新容器
applicationContext.refresh();
String[] beanNamesForType = applicationContext.getBeanNamesForType(DataSource.class);
for (String name : beanNamesForType){
System.out.println(name);
}
applicationContext.close();
}
打印輸出:
testDataSource
setActiveProfiles設置環境的時候可以傳入多個值,它的方法可以接受多個參數。
public interface ConfigurableEnvironment extends Environment, ConfigurablePropertyResolver {
void setActiveProfiles(String... var1);
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。