I. Introduction
The subject of this article is written in springboot in multiple configuration files, specify that the configuration file to take effect, in order to achieve the development environment, test environment, online environments and their applications in different configurations; you will get your way through this, learn springboot multi-environment configuration; learn to use virtual machine configuration parameters idea to start a different profile; learn to use the jar package run and specify a different configuration files;
Two ways to activate a
Internal spring provides a framework in Mode 2 is used to load YAML document for your configuration file is read when you start; YamlPropertiesFactoryBean
loads YAML become Properties; YamlMapFactoryBean
converted to YAML map; use YAML easier without worrying about how specific internal conversion;
2.1pom.xml
Introducing dependent boor-start-dependent packaging and plug; jdk1.8 version, boot2.1.1;
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2.2 application.yml
Things simple, knowledge seekers will not be written in a configuration file other content, you can specify the port, used to distinguish between different environments; where the configuration for the dev development environment, production environment for pro; enabled by default dev environment;
# 激活配置
spring:
profiles:
active: dev
---
# 配置一 开发环境
spring:
profiles: dev
server:
port: 8060
---
# 配置二 生产环境
spring:
profiles: pro
server:
port: 8061
2.3 controller
hell controller a method for testing a browser;
@RestController
public class ZSZXZ {
@GetMapping("zszxz")
public String hello(){
return "hello 知识追寻者";
}
}
2.4 startup class
Add @SpringBootApplication startup class notes, the table name is springboot application will be automatically configured;
/**
* @Author lsc
* <p> 多环境配置 </p>
*/
@SpringBootApplication
public class ProfileApp {
public static void main(String[] args) {
SpringApplication.run(ProfileApp.class,args);
}
}
2.5 dev execution results
Start project, open the browser follows the path, port 8086, the default environment has been successfully activated dev
http://localhost:8060/zszxz
2.6 pro execution results
application.yml modify the environment to activate the restart after pro engineer, modify browser port, the results of the following configuration was successful;
# 激活配置
spring:
profiles:
active: pro
http://localhost:8061/zszxz
Results of the
Three activation two
3.1 application.yml
Remove application.yml in activation, knowledge seekers idea to switch to VM configuration parameter options mode activated;
---
# 配置一 开发环境
spring:
profiles: dev
server:
port: 8060
---
# 配置二 生产环境
spring:
profiles: pro
server:
port: 8061
3.2 idea VM configuration parameters
Enter the VM options -Dspring.profiles.active=dev
, indicate activation dev environment; then restart the project can be accessed
Four project deployment
The project packaged into a jar package, execute the following command, if the package name different replace, then the browser can access in cmd;
java -jar springboot-profile-1.0-SNAPSHOT.jar --Dspring.profiles.active=dev
More than five profiles way
The above configuration files are written in a application.yml, too cumbersome, and decouple are for convenience, a plurality of recommended profile mode, then activate the main configuration file, the specific steps are as follows;
5.1 application-zszxz.yml
Yml new file named application-zszxz.yml; as follows
server:
port: 8061
5.2 application-test.yml
Yml new file named application-test.yml; as follows
server:
port: 8060
5.3 application.yml
Modify application.yml follows
spring:
profiles:
active: zszxz
5.4 Start Results
http: // localhost: After 8061 / zszxz results are consistent with previous start, no texture waste of resources;