spring profile multi environment configuration management details

  • 2020-05-30 20:18:34
  • OfStack

spring profile multi-environment configuration management

The phenomenon of

The & # 8195; The & # 8195; If you are doing some database testing at development time, you want to link to one of the test databases to avoid the impact on the development database.
The & # 8195; The & # 8195; Certain configurations at development time, such as the level of the log4j log, differ from the production environment.
The & # 8195; The & # 8195; With all of these requirements, I wish I had an easy way to switch between development environments.

To solve

The & # 8195; The & # 8195; Now spring 3.1 also brings us profile, which can be easily and quickly switched between environments.

The & # 8195; The & # 8195; It is also very convenient to use. Just add the following to applicationContext.xml and you're done


<!--  Development environment profile  -->
  <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 & # 8195; The & # 8195; profile definition 1 must be at the bottom of the document, otherwise there will be an exception. The whole xml structure looks something like this


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

Activate profile

The & # 8195; The & # 8195; spring provides us with a large number of methods to activate profile, which can be activated by code, or by system environment variables, JVM parameters, servlet context parameters to define spring.profiles.active parameters to activate profile.

1. ENV method:


ConfigurableEnvironment.setActiveProfiles("test")

2. Parameter mode of JVM:

The & # 8195; The & # 8195; In tomcat, catalina.bat (no "set" in.sh), JAVA_OPS is added. Select different profiles by setting active


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

The & # 8195; The & # 8195; Start tomcat in eclipse. Right click on run as > run configuration � > Arguments � > Added to VM arguments. local profile does not have to be uploaded to Git trace management


-Dspring.profiles.active="local"

3. web.xml:


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

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: