Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account


Spring realizes the switch between development and test environment through profile

2024-07-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >


Shulou( Report--

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 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 path: / src/main/resrouces


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 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= "-$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 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 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 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 Test / profile/ test pro / profile/ pro. Src/main/resources/filter/$ {env} .properties src/main/resources template**/** false

The default activation can be configured as follows


Filters: used to define the location of the specified filter properties file, such as the filter element assignment filters/, 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


4 、 web.xml

ContextConfigLocation classpath:spring/spring-bean.xml

5. Packing

Maven clean install-Pdev

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology


© 2024 SLNews company. All rights reserved.