spring profile來用不同的檔案,配置不同的環境, deploy不同的war
1)普通配置
如果在開發時進行一些資料庫測試,希望連結到一個測試的資料庫,以避免對開發資料庫的影響。
開發時的某些配置比如log4j日誌的級別,和生產環境又有所區別。
各種此類的需求,讓我希望有一個簡單的切換開發環境的好辦法,曾經在ROR的時候就很喜歡舒服。
現在spring3.1也給我們帶來了profile,可以方便快速的切換環境。
配置環境
只要在applicationContext.xml中新增下邊的內容,就可以了
profile的定義一定要在文件的最下邊,否則會有異常。
Java程式碼-
<beans profile="develop">
- <context:property-placeholder location="classpath*:jdbc-develop.properties"/>
- </beans>
- <beans profile="production">
- <context:property-placeholder location="classpath*:jdbc-production.properties"/>
- </beans>
-
<beans profile="test">
- <context:property-placeholder location="classpath*:jdbc-test.properties"/>
- </beans>
我通過給不同的環境,引入不同的properties來設定不同的屬性,你也可以直接在bean裡進行定義一些特殊的屬性,比如下邊這樣,在test的時候,初始化資料庫與預設資料。(程式碼摘錄:springside)
Java程式碼- <!-- unit test環境 -->
- <beans profile="test">
-
<context:property-placeholder ignore-resource-not-found="true"
- location="classpath*:/application.properties,
- classpath*:/application.test.properties" />
- <!-- Simple連線池 -->
- <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
- <property name="driverClass" value="${jdbc.driver}" />
- <property name="url" value="${jdbc.url}" />
- <property name="username" value="${jdbc.username}" />
- <property name="password" value="${jdbc.password}" />
- </bean>
- <!-- 初始化資料表結構 與預設資料-->
- <jdbc:initialize-database data-source="dataSource" ignore-failures="ALL">
- <jdbc:script location="classpath:sql/h2/schema.sql" />
- <jdbc:script location="classpath:data/import-data.sql" encoding="UTF-8"/>
- </jdbc:initialize-database>
- </beans>
切換環境
在web.xml中新增一個context-param來切換當前環境:
- <context-param>
- <param-name>spring.profiles.active</param-name>
- <param-value>develop</param-value>
- </context-param>
如果是測試類可以使用註解來切換:
Java程式碼- @ActiveProfiles("test")
測試類大概是這個樣子:
Java程式碼- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations = "classpath:applicationContext.xml")
- @ActiveProfiles("test")
- public class DictionaryServiceTest extends AbstractTransactionalJUnit4SpringContextTests
2)maven配置
pom.xml:
<profiles>
<profile>
<id>local</id>
<activation>
<activeByDefault>true</activeByDefault><!--預設是什麼-->
</activation>
<properties>
<profiles.activation>SIT</profiles.activation>
</properties>
</profile>
<profile>
<id>UAT</id>
<properties>
<profiles.activation>UAT</profiles.activation>
</properties>
</profile>
</profiles>
<dependencies>
...
</dependencies>
<build>
<span style="white-space:pre"></span><plugins>
<span style="white-space:pre"></span><plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warName>${profiles.activation}</warName>
<!-- 啟用spring profile -->
<webResources>
<resource>
<filtering>true</filtering>
<directory>src/main/webapp</directory>
<includes>
<include>**/web.xml</include>
</includes>
</resource>
</webResources>
<warSourceDirectory>src/main/webapp</warSourceDirectory>
<failMissingWebXml>fail</failMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
web.xml:
- <context-param>
- <param-name>spring.profiles.active</param-name>
- <param-value>${profiles.activation}</param-value>
- </context-param>
部署的時候:
maven build的command: clean install -p SIT,然後在/target下面就有war包了