SpringBoot specified configuration file startup project under IDEA

Table of contents

1. SpringBoot startup under idea: specify configuration file

2. The project has been packaged, run configuration

1). Use java -jar to start based on the configuration file under (1)

2) Specify other configuration files application-pro.yml in the project to start the project

3) Start the springboot project based on (3) on the Linux server

3. During the development process, idea specifies a configuration file in a certain environment to start the project


1. SpringBoot startup under idea: specify configuration file

The Springboot project has the following configuration files

The main configuration file application.yml,

Test environment: application-test.yml

Production environment: application-pro.yml

Development environment: application-dev.yml

During the development process, the actual configuration file used is usually specified in the main configuration file

spring:
    profiles:
        active: dev

2. The project has been packaged, run configuration

You need to make sure that the project has been packaged as a jar package: springboot-demo.jar

1). Use java -jar to start based on the configuration file under (1)

java -jar springboot-demo.jar

2) Specify other configuration files application-pro.yml in the project to start the project

java -jar springboot-demo.jar --spring.profiles.active=test

Another startup command (when the previous startup command fails, use the following one, or just use this one)

java -jar -Dspring.profiles.active=test springboot-demo.jar

3) Start the springboot project based on (3) on the Linux server

How to run jar

java -jar xx.jar --spring.profiles.active=dev

example

java -jar -Dspring.profiles.active=test springboot-demo.jar


 

Startup that does not show log printing

nohup java -jar -Dspring.profiles.active=test springboot-demo.jar &

3. During the development process, 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 in the figure below

Select Configuration — "Environment —" Program arguments (the main method startup method: the priority is higher than the activation in the configuration file)

// 在IDE Arguments里面添加
--spring.profiles.active=dev

 add configuration

 

Configure startup parameters 

--spring.profiles.active=test

 If you see the log shown in the figure below in the startup log, it means that the specified configuration file starts successfully.

Method 2: In addition to specifying the Profile in the configuration file and the command line, you can also specify it in the startup class, through the SpringApplication.setAdditionalProfiles method

public void setAdditionalProfiles(String... profiles) {
    this.additionalProfiles = new LinkedHashSet<String>(Arrays.asList(profiles));
}

Method 3: Select Configuration - "Environment -" VM options (JVM startup mode), and enter the following code:

-Dspring.profiles.active=dev

 

Guess you like

Origin blog.csdn.net/qq_20957669/article/details/130335711