Several ways for spring to activate profiles

activate profile

  Spring provides us with a large number of ways to activate profiles, which can be activated through code, or through system environment variables, JVM parameters, and servlet context parameters to define spring.profiles.active parameters to activate profiles. Here we implement by defining JVM parameters.

1. ENV method:

ConfigurableEnvironment.setActiveProfiles("test")

2. JVM parameter method:

  Add JAVA_OPS to catalina.bat in tomcat (without "set" in .sh). Select different profiles by setting active

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

  Start tomcat in eclipse. Right-click the project and add it to run as –> run configuration –> Arguments –> VM arguments. Local configuration files do not have to be uploaded to git tracking management

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

3. web.xml method:

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

4. Marking method (junit unit testing is very practical):
@ActiveProfiles({"unittest","productprofile"})

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326563829&siteId=291194637