Home > Java > javaTutorial > body text

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

王林
Release: 2023-05-15 23:01:13
forward
2852 people have browsed it

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
Copy after login

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
Copy after login

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
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template