spring3.1 profile configuration for different environments

Source: Internet
Author: User

Configuring the Environment

It is also convenient to use. As long as you add the following content to the Applicationcontext.xml, you can

Java code

  1. <beans profile="Develop">

  2. <context:property-placeholder location="classpath*:jdbc-develop.properties"/>

  3. </beans>

  4. <beans profile="Production">

  5. <context:property-placeholder location="classpath*:jdbc-production.properties"/>

  6. </beans>

  7. <beans profile="Test">

  8. <context:property-placeholder location="classpath*:jdbc-test.properties"/>

  9. </beans>

The definition of profile must be at the bottom of the document, or there will be an exception. The structure of the whole XML is probably like this,

Java code

    1. <beans xmlns=   >  

    2. "DataSource"  ... />  

    3.    <bean ... />  

    4.    <beans profile= >  

    5.     <bean ...>  

    6. </beans>   

I set different properties by giving different environments a different property, and you can define some special attributes directly in the bean, such as the bottom, when you initialize the database and the default data in test. (Excerpt from Code: Springside)

Java code

  1. <!--unit test environment--

  2. <beans profile="Test">

  3. <context:property-placeholder ignore-resource-not-found="true"

  4. Location= "Classpath*:/application.properties,

  5. Classpath*:/application.test.properties "/>

  6. <!--simple connection pool--

  7. <bean id="DataSource" class="Org.springframework.jdbc.datasource.SimpleDriverDataSource ">

  8. <property name="Driverclass" value="${jdbc.driver}" />

  9. <property name="url" value="${jdbc.url}" />

  10. <property name="username" value="${jdbc.username}" />

  11. <property name="password" value="${jdbc.password}" />

  12. </bean>

  13. <!--initialize data table structure with default data--

  14. <jdbc:initialize-database data-source="DataSource " ignore-failures="all">

  15. <jdbc:script location="Classpath:sql/h2/schema.sql" />

  16. <jdbc:script location="Classpath:data/import-data.sql" encoding="UTF-8"/>

  17. </jdbc:initialize-database>

  18. </beans>

switching environments

Add a Context-param in Web. XML to switch the current environment:

Java code

    1. <context-param>

    2. <param-name>spring.profiles.active</param-name>

    3. <param-value>develop</param-value>

    4. </context-param>

If the test class can use annotations to switch:

Java code

    1. @ActiveProfiles ("Test")

The test class is probably like this:

Java code

    1. @RunWith ( Springjunit4classrunner. @ Contextconfiguration (Locations = )   

    2. @ActiveProfiles ( "test" )   

    3. public  extends   abstracttransactionaljunit4springcontexttests  

You can write a base class to write this annotation, and then let your test classes inherit this test base class, which is handy.


spring3.1 profile configuration for different environments

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.