1. SpringBoot startup under idea: specify the configuration file
The Springboot project has the following configuration files
Main configuration file application.yml,
Test environment: application-test.yml
Production environment: application-pro.yml
Development environment: application-dev.yml
1.1. Configuration file specifies the environment
在主配置文件里指定实际使用的配置文件
spring:
profiles:
active: dev
1.2. The project has been packaged and run configuration
You need to ensure that the project has been packaged as a jar package: springboot-demo.jar, and specify other configuration files application-dev.yml in the project to start the project.
java -jar springboot-demo.jar --spring.profiles.active=dev
# 另一种启动命令(上一种启命令失效时, 用以下这种, 或就以这种)
java -jar -Dspring.profiles.active=dev springboot-demo.jar
1.3. Start the springboot project based on (3) on the Linux server
jar running mode
java -jar xx.jar --spring.profiles.active=dev
Example
java -jar -Dspring.profiles.active=test springboot-demo.jar
Startup without displaying log printing
nohup java -jar -Dspring.profiles.active=test springboot-demo.jar &
2. During the development process, the idea specifies a configuration file in a certain environment to start the project.
Method 1: Multiple configuration files. When starting the project, you need to modify the configuration information in the upper right corner, as shown below.
Select Configuration —> Environment —> Program arguments (main method startup mode: priority is higher than activation in the configuration file)
// Add --spring.profiles.active=dev in IDE Arguments
Add configuration
Configure startup parameters
--spring.profiles.active=test
Method 3: Select Configuration ——》Environment ——》VM options (JVM startup mode) and enter the following code:
-Dspring.profiles.active=dev
3. Which one should I choose between -Dspring.profiles.active=dev and --spring.profiles.active=dev?
The main difference between setting system properties using the -D parameter and setting command line parameters using the -- parameter is that setting system properties using the -D parameter can be used with any Java application and can set any system property, whereas using the -- parameter The method of setting command line parameters is unique to Spring Boot and can only be used to set the configuration file of a Spring Boot application.
In addition, when using the -D parameter to set system properties, you need to connect the property name and property value with the equal sign =, and when using the -- parameter to set command line parameters, you need to add a -- prefix before the property name.
Setting system properties using the -D parameter and setting command line parameters using the -- parameter are both valid ways of setting up a Spring Boot application's configuration files. You can choose one of the methods to set environment variables according to actual needs.
- System properties set using the -D parameter can be changed dynamically while the program is running;
- Command line parameters set using the -- parameter cannot be changed dynamically;
- When starting the Spring-Boot project, it is recommended to use --, such as --spring.profiles.active=dev