Home  >  Article  >  Java  >  How to use commands to dynamically specify the environment when starting a Springboot project

How to use commands to dynamically specify the environment when starting a Springboot project

王林
王林forward
2023-05-15 23:01:132849browse

Springboot starts using commands to specify the environment

We all know that springboot's yml file can configure multiple environments, and you can directly specify which environment to use in application.yml.

For example: specify the dev environment

How to use commands to dynamically specify the environment when starting a Springboot project

This is hard-coded in the configuration file.

So, how to dynamically specify it when starting the project?

In fact, you only need to add one more command at startup:

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

Introduction to Springboot startup commands

Three ways to specify commands for SpringBoot startup projects

1. Add the command

in the configuration file. Add the required command

in application.yml or application.properties as shown below: server.port=1118

How to use commands to dynamically specify the environment when starting a Springboot project

2. Add

java -jar thymeleaf.jar --server.port=9000

to the startup command line. This method can overwrite the contents of the original configuration file.

How to use commands to dynamically specify the environment when starting a Springboot project

Note:

If followed by the --server.port=9000 command. But after successful startup, it is still 1118.

The reason why the port has not changed is that SpringApplication did not pass in the variable parameter (the second parameter) when starting.

How to use commands to dynamically specify the environment when starting a Springboot project

3. Add commands to the JVM

java -Dserver.port=9000 -jar thymeleaf.jar

This method is to directly write the properties into the JVM as parameters of the JVM, regardless of whether you add it to the SpringBoot startup The port number can be modified even with the second parameter.

How to use commands to dynamically specify the environment when starting a Springboot project

SpringBoot startup common commands

##CommandDescription–server.portSpecify the server port –spring.profiles.active=devSpecify Running environment (usually dev, test, uat, prod)
Configurable properties in application.yml or application.properties can be passed in through commands.

The above is the detailed content of How to use commands to dynamically specify the environment when starting a Springboot project. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete