亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何理解DubboDefaultProperties EnvironmentPostProcessor

發布時間:2021-10-20 10:34:09 來源:億速云 閱讀:131 作者:柒染 欄目:大數據

如何理解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<String, Object> defaultProperties = createDefaultProperties(environment);
        if (!CollectionUtils.isEmpty(defaultProperties)) {
            addOrReplace(propertySources, defaultProperties);
        }
    }

    @Override
    public int getOrder() {
        return LOWEST_PRECEDENCE;
    }

    private Map<String, Object> createDefaultProperties(ConfigurableEnvironment environment) {
        Map<String, Object> defaultProperties = new HashMap<String, Object>();
        setDubboApplicationNameProperty(environment, defaultProperties);
        setDubboConfigMultipleProperty(defaultProperties);
        setDubboApplicationQosEnableProperty(defaultProperties);
        setAllowBeanDefinitionOverriding(defaultProperties);
        return defaultProperties;
    }

    private void setDubboApplicationNameProperty(Environment environment, Map<String, Object> 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<String, Object> defaultProperties) {
        defaultProperties.put(DUBBO_CONFIG_MULTIPLE_PROPERTY, Boolean.TRUE.toString());
    }

    private void setDubboApplicationQosEnableProperty(Map<String, Object> 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
     * <code>true</code> as default.
     *
     * @param defaultProperties the default {@link Properties properties}
     * @see #ALLOW_BEAN_DEFINITION_OVERRIDING_PROPERTY
     * @since 2.7.1
     */
    private void setAllowBeanDefinitionOverriding(Map<String, Object> 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<String, Object> 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<String, Object>()));
        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<String, Object>()));
        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的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

晋江市| 喜德县| 黑龙江省| 高碑店市| 丽水市| 新民市| 汉中市| 甘德县| 恩施市| 滦南县| 垦利县| 成安县| 马尔康县| 五常市| 隆回县| 台北市| 遵化市| 突泉县| 河间市| 六安市| 建始县| 内黄县| 冷水江市| 聂拉木县| 赞皇县| 称多县| 五华县| 盐亭县| 张家界市| 武冈市| 葫芦岛市| 时尚| 灵山县| 崇文区| 许昌市| 林口县| 贵德县| 德化县| 城口县| 汾西县| 德州市|