Transferred from: https://www.cnblogs.com/jason0529/p/6567373.html
Spring's profiles mechanism is a solution to a multi-environment scenario, which is more commonly configured for development and test environments.
Configuration Items
Spring's profiles has two variables to configure
- Spring.profiles.default default value, low priority. This variable is used when active is not configured.
- Spring.profiles.active a high priority that specifies which profile is used by the current container.
General Usage Statement Multi-profile
If you use spring's profiles mechanism, the first step is to configure the multi-environment instance in Applicationcontext.xml.
<beans profile= "Development" > <!--development environment, loading beans or properties files--</beans> <beans profile= "Test" > <!--test environment, load bean or properties file--</beans>
Activate profile
In the Java EE project, it is generally configured through Web. Xml.
<context-param> <param-name>spring.profiles.default</param-name> <param-value> Development</param-value></context-param>
Or
<context-param> <param-name>spring.profiles.default</param-name> <param-value> Test</param-value></context-param>
Two ways can be, generally recommend the first kind. The main reasons are: easy to expand, no specific profile is specified, can be overridden by other means, generally the development environment is the most used situation.
Test Environment Activation Profile
A __activeprofiles__ annotation is provided in spring-test to annotate the specific profile used by the current test case
@ActiveProfiles ("test") public abstract class Springtransactionaltestcase {}
Specifying profiles with Java Run parameters
The Java runtime can specify JVM memory parameters, as we all know. The form resembles the following to specify the memory settings that the Java Virtual machine initiates.
java_opts= "-xms1024m-xmx1024m -xx:permsize=512m-xx:maxpermsize=512m"
Java can also specify the environment variables to start, such as Catalina_home Ah, and so on, through the PS command, it can be clearly seen that Java when the start of different software is through the different-D parameters to achieve the effect. Tomcat Boot situation:
Root 2284 1 0 March? 00:01:54/opt/jdk/bin/java-djava.util.logging.config.file=/opt/apache-tomcat-8.0.32/conf/logging.properties- DJAVA.UTIL.LOGGING.MANAGER=ORG.APACHE.JULI.CLASSLOADERLOGMANAGER-SERVER-XMS1024M-XMX1024M-XX:PERMSIZE=512M-XX: Maxpermsize=512m-djava.endorsed.dirs=/opt/apache-tomcat-8.0.32/endorsed-classpath/opt/apache-tomcat-8.0.32/bin /bootstrap.jar:/opt/apache-tomcat-8.0.32/bin/tomcat-juli.jar-dcatalina.base=/opt/apache-tomcat-8.0.32- Dcatalina.home=/opt/apache-tomcat-8.0.32-djava.io.tmpdir=/opt/apache-tomcat-8.0.32/temp Org.apache.catalina.startup.Bootstrap start
Back to the point, then, is it possible to specify profile by this form? The answer is yes.
Modify the Tomcat startup script to modify java_opts directly:
java_opts= "-xms1024m-xmx1024m -xx:permsize=512m-xx:maxpermsize=512m-dspring.profiles.active=test"
Starting Tomcat and discovering that the entire system was started, the bean using profile=test was activated, proving the configuration to take effect.
Summarize
The use of the Java system parameter-D method reduces coupling and reduces the communication and collaboration between development and maintenance. When there are multiple deployment environments, the specified profile is deployed well in advance, which is extremely useful on the datasource designation.
Spring.profiles Multi-environment configuration best practices