In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
How to understand DubboDefaultProperties EnvironmentPostProcessor, I believe that many inexperienced people are at a loss about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Order
This paper mainly studies DubboDefaultPropertiesEnvironmentPostProcessor.
DubboDefaultPropertiesEnvironmentPostProcessor
Dubbo-spring-boot-project-2.7.3/dubbo-spring-boot-compatible/autoconfigure/src/main/java/org/apache/dubbo/spring/boot/env/DubboDefaultPropertiesEnvironmentPostProcessor.java
Public class DubboDefaultPropertiesEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered {/ * * The name of default {@ link PropertySource} defined in SpringApplication#configurePropertySources method. * / public static final String PROPERTY_SOURCE_NAME = "defaultProperties"; / * The property name of "spring.main.allow-bean-definition-overriding". * Please refer to: https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.1-Release-Notes#bean-overriding * / public static final String ALLOW_BEAN_DEFINITION_OVERRIDING_PROPERTY = "spring.main.allow-bean-definition-overriding"; @ Override public void postProcessEnvironment (ConfigurableEnvironment environment, SpringApplication application) {MutablePropertySources propertySources = environment.getPropertySources (); Map defaultProperties = createDefaultProperties (environment) If (! CollectionUtils.isEmpty (defaultProperties)) {addOrReplace (propertySources, defaultProperties);}} @ Override public int getOrder () {return LOWEST_PRECEDENCE;} private Map createDefaultProperties (ConfigurableEnvironment environment) {Map defaultProperties = new HashMap (); setDubboApplicationNameProperty (environment, defaultProperties); setDubboConfigMultipleProperty (defaultProperties); setDubboApplicationQosEnableProperty (defaultProperties); setAllowBeanDefinitionOverriding (defaultProperties); return defaultProperties } private void setDubboApplicationNameProperty (Environment environment, Map defaultProperties) {String springApplicationName = environment.getProperty (SPRING_APPLICATION_NAME_PROPERTY); if (StringUtils.hasLength (springApplicationName) & &! environment.containsProperty (DUBBO_APPLICATION_NAME_PROPERTY)) {defaultProperties.put (DUBBO_APPLICATION_NAME_PROPERTY, springApplicationName) }} private void setDubboConfigMultipleProperty (Map defaultProperties) {defaultProperties.put (DUBBO_CONFIG_MULTIPLE_PROPERTY, Boolean.TRUE.toString ());} private void setDubboApplicationQosEnableProperty (Map defaultProperties) {defaultProperties.put (DUBBO_APPLICATION_QOS_ENABLE_PROPERTY, Boolean.FALSE.toString ()) } / * * Set {@ link # ALLOW_BEAN_DEFINITION_OVERRIDING_PROPERTY "spring.main.allow-bean-definition-overriding"} to be * true as default. * * @ param defaultProperties the default {@ link Properties properties} * @ see # ALLOW_BEAN_DEFINITION_OVERRIDING_PROPERTY * @ since 2.7.1 * / private void setAllowBeanDefinitionOverriding (Map defaultProperties) {defaultProperties.put (ALLOW_BEAN_DEFINITION_OVERRIDING_PROPERTY, Boolean.TRUE.toString ()) } / * Copy from BusEnvironmentPostProcessor#addOrReplace (MutablePropertySources, Map) * * @ param propertySources {@ link MutablePropertySources} * @ param map Default Dubbo Properties * / private void addOrReplace (MutablePropertySources propertySources, Map map) {MapPropertySource target = null If (propertySources.contains (PROPERTY_SOURCE_NAME)) {PropertySource source = propertySources.get (PROPERTY_SOURCE_NAME); if (source instanceof MapPropertySource) {target = (MapPropertySource) source For (String key: map.keySet ()) {if (! target.containsProperty (key)) {target.getSource () .put (key, map.get (key)) } if (target = = null) {target = new MapPropertySource (PROPERTY_SOURCE_NAME, map);} if (! propertySources.contains (PROPERTY_SOURCE_NAME)) {propertySources.addLast (target);}
DubboDefaultPropertiesEnvironmentPostProcessor implements EnvironmentPostProcessor and Ordered interface; the postProcessEnvironment method first gets the MutablePropertySources from environment, then creates the default configuration defaultProperties through createDefaultProperties (DUBBO_APPLICATION_NAME_PROPERTY is set to springApplicationName by default, DUBBO_CONFIG_MULTIPLE_PROPERTY is set to true, and DUBBO_APPLICATION_QOS_ENABLE_PROPERTY is false). Finally, defaultProperties is overwritten to propertySources;getOrder by addOrReplace method, and LOWEST_PRECEDENCE is returned by the propertySources;getOrder method.
DubboDefaultPropertiesEnvironmentPostProcessorTest
Dubbo-spring-boot-project-2.7.3/dubbo-spring-boot-autoconfigure/src/test/java/org/apache/dubbo/spring/boot/env/DubboDefaultPropertiesEnvironmentPostProcessorTest.java
Public class DubboDefaultPropertiesEnvironmentPostProcessorTest {private DubboDefaultPropertiesEnvironmentPostProcessor instance = new DubboDefaultPropertiesEnvironmentPostProcessor (); private SpringApplication springApplication = new SpringApplication (); @ Test public void testOrder () {Assert.assertEquals (Ordered.LOWEST_PRECEDENCE, instance.getOrder ());} @ Test public void testPostProcessEnvironment () {MockEnvironment environment = new MockEnvironment (); / / Case 1: Not Any property instance.postProcessEnvironment (environment, springApplication) / / Get PropertySources MutablePropertySources propertySources = environment.getPropertySources (); / / Nothing to change PropertySource defaultPropertySource = propertySources.get ("defaultProperties"); Assert.assertNotNull (defaultPropertySource); Assert.assertEquals ("true", defaultPropertySource.getProperty ("dubbo.config.multiple"); Assert.assertEquals ("false", defaultPropertySource.getProperty ("dubbo.application.qos-enable")) / / Case 2: Only set property "spring.application.name" environment.setProperty ("spring.application.name", "demo-dubbo-application"); instance.postProcessEnvironment (environment, springApplication); defaultPropertySource = propertySources.get ("defaultProperties"); Object dubboApplicationName = defaultPropertySource.getProperty ("dubbo.application.name"); Assert.assertEquals ("demo-dubbo-application", dubboApplicationName) / / Case 3: Only set property "dubbo.application.name" / / Rest environment environment = new MockEnvironment (); propertySources = environment.getPropertySources (); environment.setProperty ("dubbo.application.name", "demo-dubbo-application"); instance.postProcessEnvironment (environment, springApplication); defaultPropertySource = propertySources.get ("defaultProperties"); Assert.assertNotNull (defaultPropertySource) DubboApplicationName = environment.getProperty ("dubbo.application.name"); Assert.assertEquals ("demo-dubbo-application", dubboApplicationName); / / Case 4: If "defaultProperties" PropertySource is present in PropertySources / / Rest environment environment = new MockEnvironment (); propertySources = environment.getPropertySources (); propertySources.addLast (new MapPropertySource ("defaultProperties", new HashMap () Environment.setProperty ("spring.application.name", "demo-dubbo-application"); instance.postProcessEnvironment (environment, springApplication); defaultPropertySource = propertySources.get ("defaultProperties"); dubboApplicationName = defaultPropertySource.getProperty ("dubbo.application.name"); Assert.assertEquals ("demo-dubbo-application", dubboApplicationName); / / Case 5: Rest dubbo.config.multiple and dubbo.application.qos-enable environment = new MockEnvironment () PropertySources = environment.getPropertySources (); propertySources.addLast (new MapPropertySource ("defaultProperties", new HashMap ()); environment.setProperty ("dubbo.config.multiple", "false"); environment.setProperty ("dubbo.application.qos-enable", "true"); instance.postProcessEnvironment (environment, springApplication); Assert.assertEquals ("false", environment.getProperty ("dubbo.config.multiple")) Assert.assertEquals ("true", environment.getProperty ("dubbo.application.qos-enable"))
The testPostProcessEnvironment method verifies the addOrReplace method of DubboDefaultPropertiesEnvironmentPostProcessor through MockEnvironment.
Summary
DubboDefaultPropertiesEnvironmentPostProcessor implements EnvironmentPostProcessor and Ordered interface; the postProcessEnvironment method first gets the MutablePropertySources from environment, then creates the default configuration defaultProperties through createDefaultProperties (DUBBO_APPLICATION_NAME_PROPERTY is set to springApplicationName by default, DUBBO_CONFIG_MULTIPLE_PROPERTY is set to true, and DUBBO_APPLICATION_QOS_ENABLE_PROPERTY is false). Finally, defaultProperties is overwritten to propertySources;getOrder by addOrReplace method, and LOWEST_PRECEDENCE is returned by the propertySources;getOrder method.
Doc
DubboDefaultPropertiesEnvironmentPostProcessor
After reading the above, have you mastered how to understand DubboDefaultProperties EnvironmentPostProcessor? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.