在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進行測試。<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>
須注意的是在XML裡面設定profile時,<beans/> element一定要放在整個XML的最下方。