最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

通過實例了解Spring中@Profile的作用

 更新時間:2019年11月20日 10:21:35   作者:聞窗  
這篇文章主要介紹了通過實例了解Spring中@Profile的作用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

這篇文章主要介紹了通過實例了解Spring中@Profile的作用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

根據(jù)系統(tǒng)環(huán)境的不同,Profile可以用來切換數(shù)據(jù)源。例如切換開發(fā),測試,生產環(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來給屬性賦值

測試:運行的時候設置環(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

也可以使用代碼的方式設置系統(tǒng)環(huán)境,創(chuàng)建容器的時候使用無參構造方法,然后設置屬性。

@Test
  public void test(){
    //創(chuàng)建容器
    AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
    //設置需要激活的環(huán)境
    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設置環(huán)境的時候可以傳入多個值,它的方法可以接受多個參數(shù)。

public interface ConfigurableEnvironment extends Environment, ConfigurablePropertyResolver {
  void setActiveProfiles(String... var1);

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

相關文章

最新評論

奉贤区| 双流县| 高安市| 海淀区| 墨竹工卡县| 剑川县| 鄂尔多斯市| 威信县| 台中市| 大冶市| 平安县| 比如县| 饶河县| 罗江县| 望都县| 鹰潭市| 浙江省| 柳河县| 桐庐县| 绥芬河市| 蒙阴县| 六枝特区| 潼关县| 河间市| 化隆| 封开县| 福泉市| 纳雍县| 嵩明县| 宜昌市| 进贤县| 壶关县| 永登县| 郑州市| 泗阳县| 定结县| 德格县| 时尚| 建湖县| 巴彦淖尔市| 木里|