原文地址:http://melin.iteye.com/blog/1339060

很早学习rails的时候,rails在服务器启动的时候,通过参数可以切换不同运行环境。也许spring从rails吸取了这样的功能,从spring3.1就提供了profile功能,方便我们为不同的profile使用不同的bean。能够想到的应用场景就是数据源的配置,在production profile中,可能通过jndi获取数据源,而在开发环境中配置jndi比较费事,使用durid配置数据源,项目发布的时候需要修改数据源的配置,比较麻烦,如果忘记修改就更惨了。 为了解决这样相关的问题,这里使用maven profile和maven resource plugin为项目提供profile功能。 

spring profile实例: 

Java代码  收藏代码
  1. <beans profile="development">  
  2.    <bean id="dataSource-mysql" class="com.alibaba.druid.pool.DruidDataSource"    destroy-method="close">  
  3.        <property name="url">  
  4.            <value>jdbc:mysql://192.168.1.100/druid-test</value>  
  5.        </property>  
  6.        <property name="username">  
  7.            <value>admin</value>  
  8.        </property>  
  9.        <property name="password">  
  10.            <value>adminpassword</value>  
  11.        </property>  
  12.        <property name="initialSize">  
  13.            <value>1</value>  
  14.        </property>  
  15.        <property name="maxActive">  
  16.            <value>20</value>  
  17.        </property>  
  18.    </bean>  
  19. </beans>  
  20.   
  21.   
  22. <beans profile="production">  
  23.     <jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/datasource"/>  
  24. </beans>  



这里我主要解决项目中三个应用场景: 
1:资源文件配置,每个环境下的属性值可能不同 
2:日志文件配置,开发环境下日志只需要在控制台显示,而production profile中是需要打到日志文件中。 
3:使用maven切换spring profile。 

完成上述功能,最主要是在pom.xml文件中添加如下一段配置: 

Java代码  收藏代码
  1.      <resources>  
  2. <resource>  
  3.     <directory>src/main/resources</directory>  
  4.             <filtering>true</filtering>  
  5. </resource>  
  6.     </resources>  


这样maven搜索src/main/resources下的所有文件,替换有类似这样变量${profiles.active}的值,其中profiles.active中的值是配置pom.xml文件,如下: 

Java代码  收藏代码
  1. <profiles>  
  2.         <profile>    
  3.             <id>development</id>    
  4.             <activation>    
  5.                 <activeByDefault>true</activeByDefault>    
  6.             </activation>  
  7.             <properties>  
  8.                 <profiles.active>development</profiles.active>  
  9.             </properties>  
  10.         </profile>  
  11.         <profile>    
  12.             <id>test</id>    
  13.             <properties>  
  14.                 <profiles.active>test</profiles.active>  
  15.             </properties>  
  16.         </profile>  
  17.         <profile>    
  18.             <id>production</id>    
  19.             <properties>  
  20.                 <profiles.active>production</profiles.active>  
  21.             </properties>  
  22.         </profile>  
  23.     </profiles>  


上面配置三种profile,默认激活development profile。 

为了解决资源文件的问题,我们提供三个资源文件,命名如下。logback-和.xml中间的值为profiles.active的值。 

Java代码  收藏代码
  1. logback-development.xml  
  2. logback-production.xml  
  3. logback-test.xml  


spring property-placeholder引用资源文件的方式如下。 

Java代码  收藏代码
  1. <context:property-placeholder location="classpath:/META-INF/res/resource-${profiles.active}.properties" />  



为了解决日志文件的问题,这里使用了logback,和资源文件一样提供四个配置文件。 

Java代码  收藏代码
  1. logback-development.xml  
  2. logback-production.xml  
  3. logback-test.xml  


在classpath下添加logback.xml。logback默认加载classpath下的logback.xml文件,配置内容如下: 

Java代码  收藏代码
  1. <configuration>  
  2.     <include resource="/META-INF/logback/logback-${profiles.active}.xml"/>  
  3. </configuration>  



激活spring profile,只要在web.xml中添加如下配置: 

Java代码  收藏代码
    1. <context-param>  
    2.         <param-name>spring.profiles.active</param-name>  
    3.         <param-value>${profiles.active}</param-value>  
    4.     </context-param>  
posted on 2014-12-04 13:12  孤剑  阅读(609)  评论(0编辑  收藏  举报