`

Spring3自定义环境配置 <beans profile="">

 
阅读更多

转:http://nassir.iteye.com/blog/1535799/

摘自springside3 
Spring 3.1的功能,以后就不用为了区分Test, Dev, Production环境,搞几个只有细微区别的application.xml, application-test.xml及引用它们的web.xml了。

首先,将applicationContext.xml中的namespace从3.0升级到3.1.xsd, 然后就可以在文件末尾加入不同环境的定义,比如不同的dataSource
  1. <beans profile="test">  
  2. <jdbc:embedded-database id="dataSource">  
  3. <jdbc:script location="classpath:com/bank/config/sql/schema.sql"/>  
  4. </jdbc:embedded-database>  
  5. </beans>  
  6.   
  7. <beans profile="production">  
  8. <jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/datasource"/>  
  9. </beans>  


2.在web.xml里,你需要定义使用的profile,最聪明的做法是定义成context-param,注意这里定义的是default值,在非生产环境,可以用系统变量"spring.profiles.active"进行覆盖。

  1. <context-param>  
  2. <param-name>spring.profiles.default</param-name>  
  3. <param-value>production</param-value>  
  4. </context-param>  

3.在其他地方进行覆盖

3.1 在development和functional test启动Jetty前设置系统变量

  1. System.setProperty("spring.profiles.active""development");  
  2. server.start()  


3.2 在用到ApplicationContext的单元测试用例中,用 @ActiveProfiles定义

  1. @ContextConfiguration(locations = { "/applicationContext.xml" })  
  2. @ActiveProfiles("test")  
  3. public class AccountDaoTest extends SpringTxTestCase {  
  4. }  


在 springside里有演示了production,development,test,functional三个环境, 大家可以根据实际情况组合自己的环境管理。另外可以与Spring的properties文件加载时可顺位覆盖的特性(放一些不在版本管理中的 xx.local.properties文件),更好的支持本地开发环境,Jenkins上的functional test等其他环境。

详情:https://github.com/springside/springside4/wiki/Spring

http://huiseyiyu.iteye.com/blog/1202401

 

详细配置与使用:http://www.cnblogs.com/chanedi/archive/2012/01/12/2320837.html

分享到:
评论

相关推荐

    05spring4_di.rar

    &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans xmlns="http://www.springframework.org/schema/beans" ... xmlns:p="http://www.springframework.org/schema/p" xmlns:c=...&lt;/beans&gt;

    Spring + Hibernate + Struts 事务配置小例子(带提示框等小技巧)

    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;property name="locations"&gt; &lt;list&gt; &lt;value&gt;WEB-INF/jdbc.properties&lt;/value&gt; &lt;/list&gt; &lt;/property&gt; &lt;/bean&gt; ...

    spring定时任务

    &lt;bean id="sayHelloJob" class="org.springframework.scheduling.quartz.JobDetailBean"&gt; &lt;property name="jobClass"&gt; &lt;value&gt;com.test.Test&lt;/value&gt; &lt;/property&gt; &lt;/bean&gt; &lt;!-- 关键在如下两个触发器的...

    springmvcmybatis

    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;property name="location" value="classpath:jdbc.properties" /&gt; &lt;/bean&gt; &lt;!-- &lt;bean id="dataSource" class="org....

    ssh 整合的实例-----员工表的增删查改

    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt; &lt;property name="dataSource" ref="mydataSource"&gt; &lt;/property&gt; &lt;property name="mappingResources"&gt; &lt;list&gt; &lt;value&gt;...

    jdbc——内嵌事务

    jdbc——内嵌事务 &lt;bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt; &lt;property name="dataSource" ref="dataSource"&gt;&lt;/property&gt; ...&lt;/beans&gt;

    spring applicationContext 配置文件

    &lt;description&gt;Spring公共配置文件&lt;/description&gt; &lt;!-- mes 的數據庫 --&gt; &lt;bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"&gt; &lt;property name="driverClass" ...

    ssh框架在application.xml中配置数据源所需jar

    &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt; &lt;property name="configLocation"&gt; &lt;value&gt;classpath:hibernate.cfg.xml&lt;/value&gt; &lt;/property&gt; &lt;/bean&gt; ...

    Spring3中配置DBCP,C3P0,Proxool,Bonecp数据源

    在Spring3中配置数据源,包括DBCP,C3P0,Proxool,Bonecp主要的数据源,里面包含这些数据源的jar文件和依赖文件及配置文件。。 如Bonecp目前听说是最快的数据源,速度是传统的c3p0的25倍, bonecp.properties文件: ...

    struts2.3+hibernate3.6+spring3.1整合的纯xml配置的小项目

    application.xml配置 ... &lt;bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"&gt; &lt;property name="sessionFactory" ref="sessionFactory"&gt;&lt;/property&gt; &lt;/bean&gt; &lt;/beans&gt;

    springjdbc

    &lt;bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /&gt; &lt;!-- apache.dbcp连接池的配置 --&gt; &lt;bean id="dataSource" class="org.apache.commons.dbcp....

    维生药业小项目 SSH简单学习项目

    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt; &lt;property name="dataSource"&gt; &lt;ref bean="dataSource"&gt;&lt;/ref&gt; &lt;/property&gt; &lt;property name=...

    SSI框架整合实例

    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;property name="location"&gt; &lt;value&gt;classpath:jdbc.properties&lt;/value&gt; &lt;/property&gt; &lt;/bean&gt; &lt;bean id="dataSource" ...

    封装通用的Spring3+Struts2+MyBatis3的CRUD+条件分页查询,Spring+Quartz调度,FunctionCharts图像化工具

    封装通用的Spring3+Struts2+MyBatis3的CRUD+条件分页查询,Spring+Quartz调度,FunctionCharts图像化工具 &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans xmlns="http://www.springframework.org/schema/beans" ...

    webservice编写文档

    webservice编写文档   &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans xmlns="http://www.springframework.org/schema/beans" ... xsi:schemaLocation="http://www.springframework.org/schema/beans ...&lt;/beans&gt;

    struts2的入门开发

    struts2的入门开发,环境配置− &lt;struts-config&gt; &lt;data-sources/&gt; − &lt;form-beans&gt; &lt;form-bean name="loginForm" type="test.form.LoginForm"/&gt; &lt;/form-beans&gt; &lt;global-exceptions/&gt; &lt;global-forwards/&gt; − &lt;action-...

    quartz 定时任务

    &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans xmlns="http://www.springframework.org/schema/beans" ... xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" ...&lt;/beans&gt;

    springmvc-ibatis

    &lt;bean class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"&gt; &lt;property name="locations"&gt; &lt;value&gt;classpath:jdbc.properties&lt;/value&gt; &lt;/property&gt; &lt;/bean&gt; &lt;!-- 配置數...

    Spring MVC 入门实例

    6 &lt;bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; 7 &lt;property name="locations"&gt; 8 &lt;list&gt; 9 &lt;value&gt;/WEB-INF/jdbc.properties&lt;/value&gt; 10 ...

Global site tag (gtag.js) - Google Analytics