Skip to content
springside edited this page Feb 28, 2012 · 24 revisions

Profile

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

  1. 首先,将applicationContext.xml中的namespace从3.0升级到3.1.xsd, 然后就可以在文件末尾加入不同环境的定义,比如不同的dataSource

     <beans profile="dev">
         <jdbc:embedded-database id="dataSource">
             <jdbc:script location="classpath:com/bank/config/sql/schema.sql"/>
             <jdbc:script location="classpath:com/bank/config/sql/test-data.sql"/>
         </jdbc:embedded-database>
     </beans>
    
     <beans profile="production">
         <jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/datasource"/>	
     </beans>
    

2.在web.xml里,你需要定义使用的profile,最聪明的做法是定义成context-param

<context-param>
	    <param-name>spring.profiles.active</param-name>
	    <param-value>test</param-value>
</context-param>

3.在用到ApplicationContext的测试用例中,也需要定义

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