1. 程式人生 > 程式設計 >通過例項瞭解Spring中@Profile的作用

通過例項瞭解Spring中@Profile的作用

這篇文章主要介紹了通過例項瞭解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);

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。