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")
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"
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