Spring Environment Configuration

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.