In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Today, the editor will share with you the relevant knowledge points about how to use SpringBoot's EnvironmentPostProcessor. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
I. background
The Apollo configuration center was used in the previous project. After docking with the Apollo configuration center, the properties of the configuration center can be used in the program, so how is this realized? When are the properties of the configuration center loaded into the program? So if we find out how this is implemented, can we load the configuration properties and the encryption and decryption functions of the configuration properties from anywhere?
II. Demand
As you can see from the figure above, our requirement is simple, that is, the properties we define ourselves need to have higher priority than those in the configuration file.
Analysis 1. When to add our own configuration properties to SpringBoot
When we want to use configuration properties in Bean, our configuration properties must be put into Spring to Environment before Bean instantiation. That is, our interface needs to be called before application context refreshed, and EnvironmentPostProcessor can implement this function.
2. Get the priority of the configuration attribute
We know that getting attributes in Spring has priority.
For example, we have the following configuration property username
├─ application.properties │ > > username=huan ├─ application-dev.properties │ > > username=huan.fu
So what is the value of username at this time? Here is a picture from Apollo to explain this problem.
Reference link: https://www.apolloconfig.com/#/zh/design/apollo-design
Spring has added ConfigurableEnvironment and PropertySource since version 3.1:
ConfigurableEnvironment
Spring's ApplicationContext will contain an Environment (implement the ConfigurableEnvironment interface)
ConfigurableEnvironment itself contains many PropertySource
PropertySource
Attribute source
Can be understood as a lot of Key-Value property configuration
As you can see from the schematic above, key has a higher priority in the initial PropertySource, and in the above example, the value of username in SpringBoot is huan.fu.
3. When to add our own configuration
From the second step to get the priority of the configuration attribute, we can see that the PropertySource is executed as soon as possible, so for our configuration to take effect, we must put it as early as possible.
As can be seen from the figure above, SpringBoot loads various configurations through EnvironmentPostProcessor, while the specific implementation is implemented by ConfigDataEnvironmentPostProcessor. So we can write an implementation class of EnvironmentPostProcessor ourselves, then execute it after ConfigDataEnvironmentPostProcessor and add it to the first place in Environment.
4. Implement 1. Introduce SpringBoot dependency 4.0.0 org.springframework.boot spring-boot-starter-parent 2.6.6 com.huan.springcloud springboot-extension-point 0.0.1-SNAPSHOT springboot-extension-point 1.8 org.springframework.boot spring-boot-starter-web 2, Configure the property vim application.propertiesusername=huan3 in application.properties, write custom properties, and add them to Spring Environment
Note:
1. If you find that there is no log output in the program, check whether the slf4j output log is used. At this time, the log cannot be output because the log system is not initialized. The solution is as follows:
SpringBoot version > = 2.4You can refer to the above figure to use DeferredLogFactory to output logs < 2.41, refer to the following link https://stackoverflow.com/questions/42839798/how-to-log-errors-in-a-environmentpostprocessor-execution 2, Core code: @ Component public class MyEnvironmentPostProcessor implements EnvironmentPostProcessor ApplicationListener {private static final DeferredLog log = new DeferredLog () @ Override public void postProcessEnvironment (ConfigurableEnvironment env, SpringApplication app) {log.error ("This should be printed") } @ Override public void onApplicationEvent (ApplicationEvent event) {log.replayTo (MyEnvironmentPostProcessor.class) 4. Make the custom configuration take effect through SPI
1. Create a new META-INF/spring.factories file under src/main/resources
2. Configuration
Org.springframework.boot.env.EnvironmentPostProcessor=\ com.huan.springcloud.extensionpoint.environmentpostprocessor.CustomEnvironmentPostProcessor5, write the test class, and output the value of the defined username attribute @ Componentpublic class PrintCustomizeEnvironmentProperty implements ApplicationRunner {private static final Logger log = LoggerFactory.getLogger (PrintCustomizeEnvironmentProperty.class); @ Value ("${username}") private String userName; @ Override public void run (ApplicationArguments args) {log.info ("the username property value obtained is: {}", userName) }} 6. Run result
Note 1. The log cannot be output.
Refer to the above 3. Write custom properties and add the solution provided in Spring Environment.
2. Check that the configuration is not valid.
Check the priority of EnvironmentPostProcessor to see if @ Order or Ordered returns the wrong priority value.
Check to see if interfaces such as EnvironmentPostProcessor or ApplicationContextInitializer or BeanFactoryPostProcessor or BeanDefinitionRegistryPostProcessor are implemented elsewhere, and the order of PropertySource is modified in this.
Understand the order in which Spring acquires attributes. 2. Get the priority of configuration attributes.
3. How to initialize the log system
The following code initializes the logging system
Org.springframework.boot.context.logging.LoggingApplicationListener above is all the content of this article "how to use SpringBoot's EnvironmentPostProcessor". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
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.