方法如下:
1、在beans.xml中定義各個環境。
<beans profile="develop">
</beans>
<beans profile="product">
每個環境如果使用了不同的配置檔案(properties檔案等)可以在環境中進行加載聲明。
該段代碼需在檔案根節點的最後一段
如
<beans profile="test">
<context:property-placeholder location="classpath*:jdbc-test.properties"/>
</beans>
2、定義屬性之外的配置,如指定資料庫bean等
<beans profile="test,develop">
<bean id="authDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
</property>
<property name="url"
value="jdbc:oracle:thin:@208.120.102.10:1522:ora11g">
<property name="username" value="user"></property>
<property name="password" value="passwd"></property>
</bean>
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver">
value="jdbc:oracle:thin:@198.121.33.7:1521:ora10g">
此處定義了不同環境下不同的資料庫連結資訊
3、web.xml中定義目前使用哪個環境
在web.xml中操作context-param節點
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>product</param-value>
</context-param>
部署時指定哪個環境為激活狀态即可。
如果進行junit測試可以使用
@ActiveProfiles({"test","develop"})
附: 如果spring的profile可以和maven的釋出共同作用就更好了,但是筆者目前還未能成功将2者結合。
配置提醒:
<beans xmlns="http://www.springframework.org/schema/beans" profile="test,develop" -----設定這個之後,資料庫隻對test,develop有效
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<!-- 定義資料連接配接池 -->
<!-- 使用spring自帶的DriverManagerDataSource方式 -->
最新内容請見作者的GitHub頁:http://qaseven.github.io/