Dynamic replacement configuration information based on profile under Spring boot

Source: Internet
Author: User

Introduction: A lot of simplification and conventions have been made in the Springboot for program development, this article will be based on spring boot to show how to do profile switching and the dynamic replacement of configuration information based on profiles in deployment.

1. Environment-based

Spring Boot 1.4.1 Release, STS, JDK 1.8

2. Create the Spring Boot project

Open STS 3.8.2, a spring official development tool. Open the file--> new--> Spring Starter Project and open the following interface:

Set the project name, directory location, Groupid,artifactid information, click Next,

Select a dependency pack, which is primarily a toolkit provided with the Spring boot project; I choose Devtool here and click Finish to complete the operation.

Next, let's look at the project directory structure for spring boot:

In STS, a project based on spring Boot adds an S identity in the upper-right corner, a new [boot] logo after the project name, and the spring boot provides the Devtool helper, so "Devtools" is also written to the project name.

Compared to MAVEN's project, add mvnw.sh and mvnw.cmd two files, these two files are actually maven wrapper file, then someone will ask, these two files have what use. Its usefulness is also very friendly, that is, no longer assume that the local has been in accordance with the MAVEN library, when the local did not install MAVEN, automatically in the background to download, automatically installed Maven, and then follow the commands and operations, is not very friendly.

3. New configuration information

Add Db.xml and test.properties two profile information, Db.xml information as follows:

<?xml version= "1.0" encoding= "UTF-8"?>
<info> @test .infos@</info>
Test.properties's information is as follows:
add.info= @test. infos@
Compared to the placeholders in Maven, you can see that the placeholders for the two are different, and Spring boot uses @key_val@ to indicate that Maven uses ${key_val} to identify the placeholders.
Let's take a look at the configuration information for the project:


Here you can see the new config directory with 2 profiles under the directory. Application.properties is the default global configuration information provided by the system.

4. Update Pom.xml file contents

   added the configuration information for resources in Pom.xml, adding filter information in profile, as follows: 

<?xml version= "1.0" encoding= "UTF-8"?> <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http ://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" http://maven.apache.org/POM/4.0.0 http:// Maven.apache.org/xsd/maven-4.0.0.xsd "> <modelVersion>4.0.0</modelVersion> <groupId> Org.cuckoo.report</groupid> <artifactId>brain</artifactId> <version>0.0.1-snapshot</ version> <packaging>jar</packaging> <name>MavenDemo</name> <description>cuckoo Threat analysis</description> <parent> <groupId>org.springframework.boot</groupId> < Artifactid>spring-boot-starter-parent</artifactid> <version>1.4.1.RELEASE</version> < Relativepath/> <!--lookup parent from repository--> </parent> <properties> <project.build.s Ourceencoding>utf-8</project.build.sourceencoding> <project.reporting.outputencodinG>utf-8</project.reporting.outputencoding> <java.version>1.8</java.version> </properties > <dependencies> <dependency> <groupId>org.springframework.boot</groupId> &LT;ARTIFAC tid>spring-boot-devtools</artifactid> </dependency> <dependency> <groupId> Org.springframework.boot</groupid> <artifactId>spring-boot-starter</artifactId> </ dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactid>sprin g-boot-starter-test</artifactid> <scope>test</scope> </dependency> </dependencies> & lt;build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artif actid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> <resources> & Lt;resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </build > <profiles> <profile> <id>test</id> <activation> <activebydefa ult>true</activebydefault> </activation> <properties> <test.infos>replacedkey</tes t.infos> </properties> <build> <filters> <filter>src/main/resources/config/db.xm
			L</filter> <filter>src/main/resources/config/test.properties</filter> </filters>
 </build> </profile> </profiles> </project>
You can see that Maven's plug-ins are spring-boot-maven-plugin to complete the introduction of the relevant components, it is not easy to introduce additional related plugins. ‘

5. Perform deployment or package

Switch to the project root, you can execute 2 sets of commands at the command line, you can perform MVN-related commands, or you can perform MVNW-related spring-boot commands, and there are several main commands for spring boot:
Spring-boot:run runs your Spring boot application. Primary startup command
Spring-boot:repackage repackages your Jar/war to be executable. Spring-boot:start and Spring-boot:stop to manage the lifecycle of your spring boot application (i.e. for integration tests ).    Spring-boot:build-info generates build information that can is used by the actuator. Access Address: http://docs.spring.io/spring-boot/docs/1.4.1.RELEASE/maven-plugin/index.html

You can also use MVNW Spring-boot:run to start the application to check if the configuration information is being replaced dynamically.

The MVN command is still consistent with the original command, where MVN package is used to package the filtering of test configuration information:

D:\DEV\WORKSPACE\MAVENDEMO&GT;MVN package-dmaven.test.skip=true [INFO] scanning for projects ...
[INFO] [INFO]------------------------------------------------------------------------[INFO] Building Mavendemo 0.0.1- SNAPSHOT [INFO]------------------------------------------------------------------------[INFO] [INFO]--- Maven-resources-plugin:2.6:resources (default-resources) @ Brain---[INFO] Using ' UTF-8 ' encoding to copy filtered Resour
Ces. [INFO] Copying 3 resources [INFO] [INFO]---maven-compiler-plugin:3.1:compile (default-compile) @ brain---[info] Changes Detec
Ted-recompiling the module! [INFO] Compiling 1 source file to D:\Dev\workspace\MavenDemo\target\classes [info] [INFO]---maven-resources-plugin:2.6: Testresources (default-testresources) @ Bra [INFO] not copying test resources [INFO] [INFO]---maven-compiler-plugin:3.1: Testcompile (Default-testcompile) @ brain--[info] not compiling test sources [INFO] [INFO]---maven-surefire-plugin:2.1 8.1:test (default-test) @Brain---[INFO] Tests are skipped.
[INFO] [INFO]---maven-jar-plugin:2.6:jar (default-jar) @ brain---[info] building Jar:d:\dev\workspace\mavendemo\target\ Brain-0.0.1-snapshot.jar [INFO] [INFO]---spring-boot-maven-plugin:1.4.1.release:repackage (default) @ Brain---[info ]------------------------------------------------------------------------[INFO] Build SUCCESS [INFO]------------- -----------------------------------------------------------[INFO] Total time:4.448 s [info] finished at: 2016-10-17t15:26:19+08:00 [INFO] Final memory:25m/208m [INFO]----------------------------------------------------- -------------------
By switching to the project root directory/targe/classes to view the configuration information, you can see that the placeholder has been replaced correctly.

And then all the operations are done.

6. Extended Reading

You can also read one of my blogs about the MAVEN profile configuration information replacement article: Maven based on Pom.xml profile to dynamically switch configuration information

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.