千家信息网

如何理解DubboDefaultProperties EnvironmentPostProcessor

发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,如何理解DubboDefaultProperties EnvironmentPostProcessor,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解
千家信息网最后更新 2026年11月03日如何理解DubboDefaultProperties EnvironmentPostProcessor

如何理解DubboDefaultProperties EnvironmentPostProcessor,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

序

本文主要研究一下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实现了EnvironmentPostProcessor, Ordered接口;postProcessEnvironment方法首先从environment获取MutablePropertySources,然后通过createDefaultProperties创建默认配置defaultProperties(这里默认设置了DUBBO_APPLICATION_NAME_PROPERTY为springApplicationName、DUBBO_CONFIG_MULTIPLE_PROPERTY为true、DUBBO_APPLICATION_QOS_ENABLE_PROPERTY为false),最后通过addOrReplace方法把defaultProperties覆盖到propertySources;getOrder方法返回的是LOWEST_PRECEDENCE

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"));    }}
  • testPostProcessEnvironment方法通过MockEnvironment验证了DubboDefaultPropertiesEnvironmentPostProcessor的addOrReplace方法

小结

DubboDefaultPropertiesEnvironmentPostProcessor实现了EnvironmentPostProcessor, Ordered接口;postProcessEnvironment方法首先从environment获取MutablePropertySources,然后通过createDefaultProperties创建默认配置defaultProperties(这里默认设置了DUBBO_APPLICATION_NAME_PROPERTY为springApplicationName、DUBBO_CONFIG_MULTIPLE_PROPERTY为true、DUBBO_APPLICATION_QOS_ENABLE_PROPERTY为false),最后通过addOrReplace方法把defaultProperties覆盖到propertySources;getOrder方法返回的是LOWEST_PRECEDENCE

doc

  • DubboDefaultPropertiesEnvironmentPostProcessor

看完上述内容,你们掌握如何理解DubboDefaultProperties EnvironmentPostProcessor的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注行业资讯频道,感谢各位的阅读!

方法 内容 接口 更多 问题 配置 束手无策 为此 原因 对此 小结 技能 篇文章 经验 行业 资讯 资讯频道 频道 研究 验证 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 互联网科技企业 广东万易互联网科技有限公司惠州分公司 湖南永感互联网科技有限公司 国智互联网科技 互联网的科技创新 苏州互联网科技贷款公司 镁科技互联网资讯平台 开启互联网科技时代小说 互联网软件和科技 赶超互联网科技 微信多开 恒生互联网科技指数跳水 玉溪新华互联网科技好不好 徐州市互联网科技有限公司 区块链结合互联网科技 山东子淼互联网科技有限公司 电子商务与互联网科技的关系 半导体行业属于互联网高科技类吗 科技和互联网行业的特点 亚光科技低轨卫星互联网 武汉亿视互联网科技有限公司 互联网黑科技 深圳九邦互联网科技有限公司 上海互联网科技有限公司 互联网 科技创新 丰台科技园 互联网 银川新华互联网科技学校地址 上海电发互联网科技有限公司 中盈科技互联网信息有限公司 玉溪互联网科技怎么选 工业互联网等科技实现智能化赋能
0