Spring Profile Multi-environment configuration management

Source: Internet
Author: User

Local, test, development, product and other different environment file configuration

Phenomenon

If you do some database testing at development time, you want to link to a test database to avoid the impact on the development database.

Some of the configuration at development time, such as the level of the log4j log, differs from the production environment.

A variety of such needs, let me want to have a simple switch development environment of a good way.

Solve

Now spring3.1 also brought us a profile, can be convenient to quickly switch the environment.

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

<!--development environment configuration file--<beans profile=  "test" > <context: Property-placeholder location= "/WEB-INF/ Test-orm.properties "/> </beans> <!--Local Environment Profile--<beans profile= "local" > < context:property-placeholder location=  "/web-inf/local-orm.properties"/> </ 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

<beans xmlns="..." ...>    <bean id="dataSource" ... />    <bean ... /> <beans profile="..."> <bean ...> </beans> </beans>
Activate profile

Spring provides us with a number of methods for activating the profile, which can be activated by code, or by defining the Spring.profiles.active parameter activation profile through system environment variables, JVM parameters, servlet context parameters, Here we implement by defining JVM parameters.

1. ENV Mode:

ConfigurableEnvironment.setActiveProfiles("test")
    • 1
    • 1

2. JVM parameter mode:

Add Java_ops to Tomcat in Catalina.bat (. SH without "set"). Select different profiles by setting active

set JAVA_OPTS="-Dspring.profiles.active=test"

Start Tomcat in Eclipse. Items are added in the right-click Run as–> run configuration–>arguments–> VM Arguments. Local profile does not have to upload git tracking management

-Dspring.profiles.active="local"
 
   
  
  • 1

3. Web. XML mode:

<init-param>  <param-name>spring.profiles.active</param-name>  <param-value>production</param-value></init-param>

4, Labeling method (JUnit unit test is very practical):
@ActiveProfiles({"unittest","productprofile"})

Spring Profile Multi-environment configuration management

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.