In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what is the principle of loading bootstrap.properties files in SpringBoot". In daily operation, I believe that many people have doubts about the principle of loading bootstrap.properties files in SpringBoot. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts of "what is the principle of loading bootstrap.properties files in SpringBoot?" Next, please follow the editor to study!
for SpringBoot properties files believe that we use more in the work, for application.properties and application.yml files should be very familiar with, but for bootstrap.properties files and bootstrap.yml these two files with less estimate, used should be clear that the file information defined in bootstrap.properties will first be loaded with information in application.properties. And when you use it, you often encounter the trouble of not getting the information defined in bootstrap.properties. This article will solve these mysteries for you.
The use of 1.bootstrap
first does not support bootstrap.properties properties files by default in SpringBoot. We need to map the dependencies of SpringCloud.
Related version environment
Then create the corresponding bootstrap.properties file, of course, you can also create the bootstrap.yml file
Synchronized we will also create an application.properties file that overrides an attribute
Then we get the test in controller
Access test: http://localhost:8080/query
Through the visit, we can see that the information in the bootstrap.properties has been obtained, and the age has been overwritten by the attributes in the application.properties. What is the loading order? Why would it be covered? Let's analyze it next.
Analysis of 2.bootstrap loading principle
Before read this article, it's best to take a look at the listening mechanism in SpringBoot that I explained earlier.
2.1 BootstrapApplicationListener
When uses bootstrap.properties files, we need to map the relevant dependencies
actually provides us with a new listener, the BootstrapApplicationListener listener, in the corresponding spring.factories file in this dependency.
The event triggered by and BootstrapApplicationListener snooping is the ApplicationEnvironmentPreparedEvent event, which is actually the same event as ConfigFileApplicationListener, the listener we described earlier when listening to application.properties.
if you read the previous article, then you will feel a little bit better here. That is, when the corresponding event is issued when it is started, the listener will trigger the relevant parsing behavior.
2.2 start the process carding
After has figured out the relationship between the listeners, let's take a look at how the initiated process code is executed.
Direct access
In the construction method of SpringApplication, we should pay attention to two points, 1. The main class record of the listener's load 2.main method
Then come back and enter the run method
Debug to the first endpoint.
And then we let it go.
From the above motion picture, you can see that the run method has been entered again. Let's first look at the results of the processing.
And then we'll let it go. Go on.
Load in two stages, first on the right and then on the right. So what is the principle of the first load in this? Continue to analyze.
2.3 loading principle of bootstrap.properties
, let's see if a parent context appears to load our bootstrap.properteis file first, or does it start with this diagram?
The link can be tracked step by step as above.
Skip the non-critical ones and go straight to BootstrapApplicationListener.
Then go into the bootstrapServiceContext method.
Here we see that a SpringApplication object has been created. This is actually the parent Context object.
If you enter the run method, you will find that you are going back to the front.
At this point, the study of "what is the principle of loading bootstrap.properties files in SpringBoot" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.