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.           
  7.         <!-- Simple连接池 -->  
  8.         <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">  
  9.             <property name="driverClass" value="${jdbc.driver}" />  
  10.             <property name="url" value="${jdbc.url}" />  
  11.             <property name="username" value="${jdbc.username}" />  
  12.             <property name="password" value="${jdbc.password}" />  
  13.         </bean>  
  14.   
  15.         <!-- 初始化数据表结构 与默认数据-->  
  16.         <jdbc:initialize-database data-source="dataSource" ignore-failures="ALL">  
  17.             <jdbc:script location="classpath:sql/h2/schema.sql" />  
  18.             <jdbc:script location="classpath:data/import-data.sql" encoding="UTF-8"/>  
  19.         </jdbc:initialize-database>  
  20.     </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包了

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值