亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

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

根據系統環(huán)境的不同,Profile可以用來切換數據源。例如切換開發(fā),測試,生產環(huán)境的數據源。

舉個例子:

先創(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

也可以使用代碼的方式設置系統環(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)境的時候可以傳入多個值,它的方法可以接受多個參數。

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

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

相關文章

  • Springboot?No?bean?named?'XXXXX'?available?問題解決方法

    Springboot?No?bean?named?'XXXXX'?available?問

    這篇文章主要介紹了Springboot?No?bean?named?'XXXXX'?available?問題解決方法,解決方法也很簡單,盡量規(guī)范類的命名,注解中指定bean名稱,本文給大家介紹的非常詳細,需要的朋友可以參考下
    2023-07-07
  • SpringBoot輕松實現ip解析(含源碼)

    SpringBoot輕松實現ip解析(含源碼)

    IP地址一般以數字形式表示,如192.168.0.1,IP解析是將這個數字IP轉換為包含地區(qū)、城市、運營商等信息的字符串形式,如“廣東省深圳市 電信”,這樣更方便人理解和使用,本文給大家介紹了SpringBoot如何輕松實現ip解析,需要的朋友可以參考下
    2023-10-10
  • Springboot swagger配置過程詳解(idea社區(qū)版2023.1.4+apache-maven-3.9.3-bin)

    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
  • 基于Mybatis-Plus的CRUD的實現

    基于Mybatis-Plus的CRUD的實現

    這篇文章主要介紹了基于Mybatis-Plus的CRUD的實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-11-11
  • Java判空的一些常見方法

    Java判空的一些常見方法

    這篇文章主要給大家分享介紹了Java判空的一些常見方法,在程序中必須進行嚴格的判空處理,避免對空對象的異常操作,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-07-07
  • JAVA的反射機制你了解多少

    JAVA的反射機制你了解多少

    這篇文章主要為大家詳細介紹了JAVA的反射機制,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • SpringBoot2.1.x,創(chuàng)建自己的spring-boot-starter自動配置模塊操作

    SpringBoot2.1.x,創(chuàng)建自己的spring-boot-starter自動配置模塊操作

    這篇文章主要介紹了SpringBoot2.1.x,創(chuàng)建自己的spring-boot-starter自動配置模塊操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • 深度解析Spring?Filter方法示例

    深度解析Spring?Filter方法示例

    這篇文章主要為大家介紹了深度解析Spring?Filter用法示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-08-08
  • Spring JPA自定義查詢結果的接收方式

    Spring JPA自定義查詢結果的接收方式

    這篇文章主要介紹了Spring JPA自定義查詢結果的接收方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • Spring?@Cacheable注解類內部調用失效的解決方案

    Spring?@Cacheable注解類內部調用失效的解決方案

    這篇文章主要介紹了Spring?@Cacheable注解類內部調用失效的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01

最新評論