In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how the multi-environment configuration scheme in springboot is, and the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
A preface
The theme of this article is to write multiple configuration files in springboot and specify to let each configuration file take effect, so that it can be flexibly applied in the development environment, test environment, and online environment according to different configurations; after reading this article, you will get and learn the multi-environment configuration of springboot; learn to start different configuration files using idea configuration virtual machine parameters; learn to run with jar packages and specify different configuration files, etc.
Two activation mode one
The spring framework provides two ways to load YAML documents for reading configuration files at startup; YamlPropertiesFactoryBean loads YAML into Properties;YamlMapFactoryBean, converts YAML to map; and makes it easier to use YAML without worrying about how to convert it internally
2.1pom.xml
Introduction of dependency boor-start dependencies and packaging plug-ins; jdk1.8 version, boot2.1.1
Org.springframework.boot spring-boot-starter-parent 2.1.1.RELEASE org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-maven-plugin
2.2 application.yml
Everything is simple, knowledge seekers will not write anything else in the configuration file, just specify the port to distinguish different environments; the configuration development environment is dev, and the production environment is pro; to activate the dev environment by default
# Activation configuration spring: profiles: active: dev---# configuration one development environment spring: profiles: devserver: port: 8060 Murray # configuration two production environment spring: profiles: proserver: port: 8061
2.3 controller
Controller a hell method for browser testing
@ RestControllerpublic class ZSZXZ {@ GetMapping ("zszxz") public String hello () {return "hello knowledge seeker";}}
2.4 Startup class
Add @ SpringBootApplication annotation to the startup class. The table name is a springboot application and will be configured automatically.
/ * * @ Author lsc *
Multi-environment configuration
* / @ SpringBootApplicationpublic class ProfileApp {public static void main (String [] args) {SpringApplication.run (ProfileApp.class,args);}}
2.5 dev execution result
Start the project and open the browser path as follows. The port is 8086, which means that the dev environment has been activated successfully by default.
Http://localhost:8060/zszxz
2.6 pro execution result
In application.yml, restart the project after changing the active environment to pro, and modify the browser port. The following shows that the configuration is successful.
# Activation configuration spring: profiles: active: pro
Http://localhost:8061/zszxz
Execution result
Three activation modes two
3.1 application.yml
Remove the activation mode in application.yml, and knowledge seekers use the idea configuration VM parameter option to activate.
-# configuration 1 development environment spring: profiles: devserver: port: 8060 Murray Meli # configuration 2 production environment spring: profiles: proserver: port: 8061
3.2 idea configuration VM parameters
Enter-Dspring.profiles.active=dev in the VM option to activate the dev environment, and then restart the project access
4 deployment mode of the project
Package the project into a jar package, execute the following command in cmd. If the package name is different, replace it, and then access it by the browser.
Java-jar springboot-profile-1.0-SNAPSHOT.jar-- Dspring.profiles.active=dev
Wudui profile mode
The above configuration files are written in an application.yml, which is too cumbersome. In order to facilitate access to decoupling, multiple configuration files are recommended, and then activated in the main configuration file. The specific steps are as follows
5.1 application-zszxz.yml
Create a new yml file named application-zszxz.yml;. The content is as follows
Server: port: 8061
5.2 application-test.yml
Create a new yml file named application-test.yml;. The content is as follows
Server: port: 8060
5.3 application.yml
Modify the content of application.yml as follows
Spring: profiles: active: zszxz
5.4 results after startup
After http://localhost:8061/zszxz starts, the result is the same as before, no longer waste of resources.
About how the multi-environment configuration scheme in springboot is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.