Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Spring realizes the switch between development and test environment through profile

Shulou Source: shulou.com Published: 2022-06-03 08:20:32 10月03日 Update

Taking development testing as an example, this paper introduces the switching between test environment and development environment by using profile under tomcat deployment application and maven deployment application.

I. tomcat deployment applications

1. Data source configuration

Dev.properties path: / src/main/resrouces

Jdbc.database=MYSQLjdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://mysql:3306/develop?useUnicode=true&characterEncoding=utf-8jdbc.schema=developjdbc.username=rootjdbc.password=12qw4ds

Test.properties path: / src/main/resrouces

Jdbc.database=MYSQLjdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8jdbc.schema=testjdbc.username=rootjdbc.password=123456

ApplicationContext-detabase.xml path: src/main/resources/spring

2 、 springmvc.xml webapp/WEB-INF

You can separate the data source configuration of a development and production environment by defining a profile

2. Define the default profile in web.xml:

The default profile means that the content defined in the default profile will be used when no profile is activated. You can usually define the global servlet context parameter spring.profiles.default implementation in web.xml.

Spring.profiles.default development

4. Activate profile

Spring provides us with a large number of methods to activate profile, which can be activated by code, or through system environment variables, JVM parameters, servlet context parameters to determine spring.profiles.active parameters to activate profile, here we define JVM parameters. Taking tomcat as an example, we add the following JVM parameters to the startup script of tomcat

JAVA_OPTS= "- Dspring.profiles.active=development-server-XX:PermSize=256M-XX:MaxPermSize=512M-Xms1024M-Xmx1024M-Xss512k-XX:LargePageSizeInBytes=128m-XX:MaxTenuringThreshold=15-XX:+AggressiveOpts-XX:+UseBiasedLocking-XX:+DisableExplicitGC-XX:+UseConcMarkSweepGC-XX:+UseParNewGC-XX:+CMSParallelRemarkEnabled-XX:+UseFastAccessorMethods-XX:+UseCMSInitiatingOccupancyOnly-XX:+HeapDumpOnOutOfMemoryError-XX:HeapDumpPath=$CATALINA_BASE/heap.dump.bin-Djava.awt.headless=true"

If it is not defined, the default profile we specified will be used

II. Maven deployment applications

1. Configuration file

The dev.properties path is / src/main/resources/filter

Master.jdbc.driverClass = com.mysql.jdbc.Drivermaster.jdbc.url = jdbc:mysql://mysql-dev:3306/devmaster.jdbc.user = rootmaster.jdbc.password = Aa12345678

The test.properties path is / src/main/resources/filter

Master.jdbc.driverClass = com.mysql.jdbc.Drivermaster.jdbc.url = jdbc:mysql://mysql-test:3306/testmaster.jdbc.user = rootmaster.jdbc.password = root

Config.properties path: / src/main/resource/META-INF

Master.jdbc.driverClass = ${master.jdbc.driverClass} master.jdbc.url = ${master.jdbc.url} master.jdbc.user = ${master.jdbc.user} master.jdbc.password = ${master.jdbc.password}

The spring-datasource.xml path is: / src/main/resources/spring

2 、 pom.xml

Dev true / profile/dev.properties dev Test / profile/test.properties test pro / profile/pro.properties pro. Src/main/resources/filter/$ {env} .properties src/main/resources template**/** false

The default activation can be configured as follows

True

Filters: used to define the location of the specified filter properties file, such as the filter element assignment filters/filter1.properties, then the name=value pair can be defined in this file, and the value of this name=value pair can be referenced by ${name} in the project pom. The default filter directory is ${basedir} / src/main/filters/.

Resources: describes the location of resources in the project

3 、 spring-bean.xml

Classpath:/META-INF/config.properties

4 、 web.xml

ContextConfigLocation classpath:spring/spring-bean.xml

5. Packing

Maven clean install-Pdev

Tags: Path parameter activation environment application configuration can be passed file development test up and down context location project data source switch code to development element Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Tech Info Huawei NVidia MySQL