2013年1月16日 星期三

Spring Profile

在Spring 3.1之後有支援profile的設定,官方部落格有這篇文章可以參考:SPRING 3.1 M1: INTRODUCING @PROFILE

在Cloud Foundry也支援Spring Profiles:Using Spring Profiles to Conditionalize Cloud Foundry Configuration

實務上我們用Spring Profile用來設定不同環境下的PropertyPlaceholderConfigurer,例如:
Example:
<beans profile="dev">
  <bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
      <list>
        <value>classpath:jdbc-dev.properties</value>
        <value>classpath:cornerstone-service-dev.properties</value>
      </list>
    </property>
  </bean>
</beans>

<beans profile="test">
  <bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
      <list>
        <value>classpath:jdbc-test.properties</value>
        <value>classpath:cornerstone-service-test.properties</value>
      </list>
    </property>
  </bean>
</beans>

<beans profile="prod">
  <bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
      <list>
        <value>classpath:jdbc-prod.properties</value>
        <value>classpath:cornerstone-service-prod.properties</value>
      </list>
    </property>
  </bean>
</beans>
或是在Test Case裡面利用org.springframework.test.context.ActiveProfiles啟動不同的profile進行測試。

須注意的是在XML裡面設定profile時,<beans/> element一定要放在整個XML的最下方。