91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

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

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

dubbo中DubboComponentScanRegistrar的作用是什么

發布時間:2021-06-22 15:03:50 來源:億速云 閱讀:237 作者:Leah 欄目:大數據

dubbo中DubboComponentScanRegistrar的作用是什么,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。

DubboComponentScanRegistrar

dubbo-2.7.3/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboComponentScanRegistrar.java

public class DubboComponentScanRegistrar implements ImportBeanDefinitionRegistrar {

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {

        Set<String> packagesToScan = getPackagesToScan(importingClassMetadata);

        registerServiceAnnotationBeanPostProcessor(packagesToScan, registry);

        registerReferenceAnnotationBeanPostProcessor(registry);

    }

    /**
     * Registers {@link ServiceAnnotationBeanPostProcessor}
     *
     * @param packagesToScan packages to scan without resolving placeholders
     * @param registry       {@link BeanDefinitionRegistry}
     * @since 2.5.8
     */
    private void registerServiceAnnotationBeanPostProcessor(Set<String> packagesToScan, BeanDefinitionRegistry registry) {

        BeanDefinitionBuilder builder = rootBeanDefinition(ServiceAnnotationBeanPostProcessor.class);
        builder.addConstructorArgValue(packagesToScan);
        builder.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
        AbstractBeanDefinition beanDefinition = builder.getBeanDefinition();
        BeanDefinitionReaderUtils.registerWithGeneratedName(beanDefinition, registry);

    }

    /**
     * Registers {@link ReferenceAnnotationBeanPostProcessor} into {@link BeanFactory}
     *
     * @param registry {@link BeanDefinitionRegistry}
     */
    private void registerReferenceAnnotationBeanPostProcessor(BeanDefinitionRegistry registry) {

        // Register @Reference Annotation Bean Processor
        BeanRegistrar.registerInfrastructureBean(registry,
                ReferenceAnnotationBeanPostProcessor.BEAN_NAME, ReferenceAnnotationBeanPostProcessor.class);

    }

    private Set<String> getPackagesToScan(AnnotationMetadata metadata) {
        AnnotationAttributes attributes = AnnotationAttributes.fromMap(
                metadata.getAnnotationAttributes(DubboComponentScan.class.getName()));
        String[] basePackages = attributes.getStringArray("basePackages");
        Class<?>[] basePackageClasses = attributes.getClassArray("basePackageClasses");
        String[] value = attributes.getStringArray("value");
        // Appends value array attributes
        Set<String> packagesToScan = new LinkedHashSet<String>(Arrays.asList(value));
        packagesToScan.addAll(Arrays.asList(basePackages));
        for (Class<?> basePackageClass : basePackageClasses) {
            packagesToScan.add(ClassUtils.getPackageName(basePackageClass));
        }
        if (packagesToScan.isEmpty()) {
            return Collections.singleton(ClassUtils.getPackageName(metadata.getClassName()));
        }
        return packagesToScan;
    }

}
  • DubboComponentScanRegistrar實現了ImportBeanDefinitionRegistrar接口定義的registerBeanDefinitions方法;它首先通過getPackagesToScan方法從AnnotationMetadata讀取要掃描的包,然后再執行registerServiceAnnotationBeanPostProcessor、registerReferenceAnnotationBeanPostProcessor方法

  • getPackagesToScan方法讀取DubboComponentScan注解信息,獲取要掃描的包;registerServiceAnnotationBeanPostProcessor方法則是通過BeanDefinitionReaderUtils.registerWithGeneratedName注冊ServiceAnnotationBeanPostProcessor

  • registerReferenceAnnotationBeanPostProcessor方法則是通過BeanRegistrar.registerInfrastructureBean注冊ReferenceAnnotationBeanPostProcessor

DubboComponentScanRegistrarTest

dubbo-2.7.3/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/DubboComponentScanRegistrarTest.java

public class DubboComponentScanRegistrarTest {

    @BeforeEach
    public void setUp() {
        ConfigManager.getInstance().clear();
    }

    @AfterEach
    public void tearDown() {
        ConfigManager.getInstance().clear();
    }

    @Test
    public void test() {

        AnnotationConfigApplicationContext providerContext = new AnnotationConfigApplicationContext();

        providerContext.register(ProviderConfiguration.class);

        providerContext.refresh();

        DemoService demoService = providerContext.getBean(DemoService.class);

        String value = demoService.sayName("Mercy");

        Assertions.assertEquals("Hello,Mercy", value);

        Class<?> beanClass = AopUtils.getTargetClass(demoService);

        // DemoServiceImpl with @Transactional
        Assertions.assertEquals(DemoServiceImpl.class, beanClass);

        // Test @Transactional is present or not
        Assertions.assertNotNull(findAnnotation(beanClass, Transactional.class));

        AnnotationConfigApplicationContext consumerContext = new AnnotationConfigApplicationContext();

        consumerContext.register(ConsumerConfiguration.class);

        consumerContext.refresh();

        ConsumerConfiguration consumerConfiguration = consumerContext.getBean(ConsumerConfiguration.class);

        demoService = consumerConfiguration.getDemoService();

        value = demoService.sayName("Mercy");

        Assertions.assertEquals("Hello,Mercy", value);

        ConsumerConfiguration.Child child = consumerContext.getBean(ConsumerConfiguration.Child.class);

        // From Child

        demoService = child.getDemoServiceFromChild();

        Assertions.assertNotNull(demoService);

        value = demoService.sayName("Mercy");

        Assertions.assertEquals("Hello,Mercy", value);

        // From Parent

        demoService = child.getDemoServiceFromParent();

        Assertions.assertNotNull(demoService);

        value = demoService.sayName("Mercy");

        Assertions.assertEquals("Hello,Mercy", value);

        // From Ancestor

        demoService = child.getDemoServiceFromAncestor();

        Assertions.assertNotNull(demoService);

        value = demoService.sayName("Mercy");

        Assertions.assertEquals("Hello,Mercy", value);

        providerContext.close();
        consumerContext.close();


    }


}
  • DubboComponentScanRegistrarTest分別驗證了providerContext、consumerContext

小結

  • DubboComponentScanRegistrar實現了ImportBeanDefinitionRegistrar接口定義的registerBeanDefinitions方法;它首先通過getPackagesToScan方法從AnnotationMetadata讀取要掃描的包,然后再執行registerServiceAnnotationBeanPostProcessor、registerReferenceAnnotationBeanPostProcessor方法

  • getPackagesToScan方法讀取DubboComponentScan注解信息,獲取要掃描的包;registerServiceAnnotationBeanPostProcessor方法則是通過BeanDefinitionReaderUtils.registerWithGeneratedName注冊ServiceAnnotationBeanPostProcessor

  • registerReferenceAnnotationBeanPostProcessor方法則是通過BeanRegistrar.registerInfrastructureBean注冊ReferenceAnnotationBeanPostProcessor

看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。

向AI問一下細節

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

AI

广汉市| 泸定县| 泉州市| 简阳市| 黄平县| 萍乡市| 潞城市| 辽阳县| 汶川县| 汤原县| 广安市| 德惠市| 连平县| 和平县| 左贡县| 桓仁| 寿宁县| 科尔| 天祝| 孝感市| 敦煌市| 任丘市| 抚远县| 凭祥市| 沙坪坝区| 绥德县| 翁源县| 都江堰市| 蒲城县| 安多县| 平阴县| 桐乡市| 崇礼县| 蒙自县| 城口县| 牙克石市| 东乌珠穆沁旗| 合川市| 济南市| 大兴区| 灵川县|