1. 程式人生 > >spring profile來用不同的檔案,配置不同的環境, deploy不同的war

spring profile來用不同的檔案,配置不同的環境, deploy不同的war

1)普通配置

如果在開發時進行一些資料庫測試,希望連結到一個測試的資料庫,以避免對開發資料庫的影響。

開發時的某些配置比如log4j日誌的級別,和生產環境又有所區別。

各種此類的需求,讓我希望有一個簡單的切換開發環境的好辦法,曾經在ROR的時候就很喜歡舒服。

現在spring3.1也給我們帶來了profile,可以方便快速的切換環境。

配置環境

只要在applicationContext.xml中新增下邊的內容,就可以了

profile的定義一定要在文件的最下邊,否則會有異常。

Java程式碼  收藏程式碼
  1.     <beans profile="develop">  
  2.         <context:property-placeholder location="classpath*:jdbc-develop.properties"/>  
  3.     </beans>  
  4.     <beans profile="production">  
  5.         <context:property-placeholder location="classpath*:jdbc-production.properties"/>  
  6.     </beans>  
  7.     <beans profile="test">  
  8.         <context:property-placeholder location="classpath*:jdbc-test.properties"/>  
  9.     </beans>  

 我通過給不同的環境,引入不同的properties來設定不同的屬性,你也可以直接在bean裡進行定義一些特殊的屬性,比如下邊這樣,在test的時候,初始化資料庫與預設資料。(程式碼摘錄:springside)

Java程式碼  收藏程式碼
  1. <!-- unit test環境 -->  
  2.     <beans profile="test">  
  3.         <context:property-placeholder ignore-resource-not-found="true"
      
  4.             location="classpath*:/application.properties,  
  5.                       classpath*:/application.test.properties" />      
  6.         <!-- Simple連線池 -->  
  7.         <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">  
  8.             <property name="driverClass" value="${jdbc.driver}" />  
  9.             <property name="url" value="${jdbc.url}" />  
  10.             <property name="username" value="${jdbc.username}" />  
  11.             <property name="password" value="${jdbc.password}" />  
  12.         </bean>  
  13.         <!-- 初始化資料表結構 與預設資料-->  
  14.         <jdbc:initialize-database data-source="dataSource" ignore-failures="ALL">  
  15.             <jdbc:script location="classpath:sql/h2/schema.sql" />  
  16.             <jdbc:script location="classpath:data/import-data.sql" encoding="UTF-8"/>  
  17.         </jdbc:initialize-database>  
  18.     </beans>  

 切換環境

在web.xml中新增一個context-param來切換當前環境:

  1. <context-param>  
  2.     <param-name>spring.profiles.active</param-name>  
  3.     <param-value>develop</param-value>  
  4. </context-param>  

 如果是測試類可以使用註解來切換:

Java程式碼  收藏程式碼
  1. @ActiveProfiles("test")  

 測試類大概是這個樣子:

Java程式碼  收藏程式碼
  1. @RunWith(SpringJUnit4ClassRunner.class)  
  2. @ContextConfiguration(locations = "classpath:applicationContext.xml")  
  3. @ActiveProfiles("test")  
  4. 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:

  1. <context-param>  
  2.     <param-name>spring.profiles.active</param-name>  
  3.     <param-value>${profiles.activation}</param-value>  
  4. </context-param>  

部署的時候:

maven build的command: clean install -p SIT,然後在/target下面就有war包了