Spring.profiles Multi-environment configuration best practices

Source: Internet
Author: User

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

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.