1. 程式人生 > >Spring 配置profile bean

Spring 配置profile bean

@Profile 註解可以根據配置來決定建立哪個bean,用來切換環境

@Configuration

@Profile("dev")

publi  class  DevelopmentProfileConfig{

      @Bean(destroyMethod = "shutdown")

     public  DataSource   dataSource(){

          return .....;

     }

}

@Profile用在類上面,它會告訴Spring這個配置類中的bean只有在dev profile啟用時才會建立。如果沒有啟用,則帶有@Bean註解的方法會被忽略

在Spring3.1中,只能將@Profile註解用在類級別上,在3.2版本開始,也可以將它用在方法級別上,與@Bean註解一同使用,這樣就能在同一個類中有多個配置。如

@Configuration

publi  class  DevelopmentProfileConfig{

      @Bean(destroyMethod = "shutdown")

      @Profile("dev")

     public  DataSource   devDataSource(){

          return .....;

     }

        @Bean(destroyMethod = "shutdown")

        @Profile("prod") 

   public  DataSource   proDataSource(){

          return .....;

     }

}

這裡雖然多個bean宣告在一個profile中,但是隻有當profile啟用時,相應的bean才會被建立。沒有宣告profile的bean都會被建立。

XML中配置profile

如下,配置xml檔案為dev環境

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:c="http://www.springframework.org/schema/c"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd"
     profile = "dev">


    <jdbc:embended-database   id="dataSource">
      ....
    </jdbc:embedded-database>
</beans>

如下,配置對應的bean為對應的環境

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:c="http://www.springframework.org/schema/c"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd"
     profile = "dev">


    <beans  profile="dev">   
     <jdbc:embended-database   id="dataSource">
          ....
        </jdbc:embedded-database>
    </beans>

   <beans  profile="qa">   
     <jdbc:embended-database   id="dataSource">
          ....
        </jdbc:embedded-database>
    </beans>
</beans>

啟用Profile

Spring在確定那個profile處於啟用狀態時,需要依賴spring.profiles.active 和 spring.profiles.default。如果設定spring.profiles.active屬性的話,那麼它的值就會被用來確定啟用那個profile.如果沒有設定spring.profiles.active,Spring將會查詢spring.profiles.default的值。如果兩個都沒有設定,那麼就沒有要啟用的profile,因此只會建立沒有定義@profile的bean。

設定這兩個屬性的方式:

1、作為DispatcherServlet的初始化引數

在web.xml中增加以下程式碼:

  <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
            <param-name>spring.profiles.default</param-name>
            <param-value>dev</param-value> //為servlet設定預設的profile
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

2、作為Web應用的上下文

在web.xml中增加以下程式碼,為上下文設定預設的profile

<context-param>
    <param-name>spring.profiles.default</param-name>
    <param-value>dev</param-value>
</context-param>

3、作為環境變數

4、作為JVM的系統屬性

5、在整合測試類上,使用@ActiveProfiles註解設定

@Runwith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(classes={PersistenceTestConfig.class})

@ActiveProfiles("dev")

public   class   PersistenceTest{

......

}

按照上面1、2方式進行設定,所有開發人員獲取到程式碼,都使用的是開發環境,無需其他額外的配置。當應用程式部署到QA,生產或者其他環境中,負責部署的人根據情況使用系統屬性,環境變數或者JNDI設定spring.profiles.active即可,當設定spring.profiles.active以後,至於spring.profiles.default設定成什麼已經無所謂了,系統優先使用spring.profiles.active中設定的profile.

active和default都使用的是profiles複數形式,說明你可以同時啟用多個profile.但是這樣可能意義不大。