The study of Spring3 found a very useful feature: Environment configuration (spring2 whether this feature is unknown)
The official sample code is as follows:
<!--App-config.xml -<Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:jdbc= "Http://www.springframework.org/schema/jdbc"Xmlns:jee= "Http://www.springframework.org/schema/jee"xsi:schemalocation="..."> <BeanID= "Transferservice"class= "Com.bank.service.internal.DefaultTransferService"> <Constructor-argref= "Accountrepository" /> <Constructor-argref= "Feepolicy" /> </Bean> <BeanID= "Accountrepository"class= "Com.bank.repository.internal.JdbcAccountRepository"> <Constructor-argref= "DataSource" /> </Bean> <BeanID= "Feepolicy"class= "Com.bank.service.internal.ZeroFeePolicy" /> <Beans Profile= "Dev"> <Jdbc:embedded-databaseID= "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-lookupID= "DataSource"Jndi-name= "Java:comp/env/jdbc/datasource" /> </Beans></Beans>
PackageCom.bank.service; the @RunWith (Springjunit4classrunner.class)//ApplicationContext'll be loaded from "Classpath:/app-config.xml"@ContextConfiguration ("/app-config.xml") @ActiveProfiles ("Dev") Public classtransferservicetest {@AutowiredPrivateTransferservice Transferservice; @Test Public voidTesttransferservice () {//Test the Transferservice}}
In addition to switching the development, deployment environment, it can also easily switch between different databases. In combat I found this testcase can also be inherited, its configuration will also be inherited, so I now practice is to write a basic testcase, configuration Good contextconfiguration, Activeprofiles, Other testcase inherit the base testcase.
But when I was integrated into a web environment, I couldn't find a way to switch this environment configuration in XML, and I found it for a long time, adding:
<Context-param> <Param-name>Spring.profiles.active</Param-name> <Param-value>Dev</Param-value></Context-param>
Spring Environment Configuration