您好,登錄后才能下訂單哦!
dubbo中DubboComponentScanRegistrar的作用是什么,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
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實(shí)現(xiàn)了ImportBeanDefinitionRegistrar接口定義的registerBeanDefinitions方法;它首先通過getPackagesToScan方法從AnnotationMetadata讀取要掃描的包,然后再執(zhí)行registerServiceAnnotationBeanPostProcessor、registerReferenceAnnotationBeanPostProcessor方法
getPackagesToScan方法讀取DubboComponentScan注解信息,獲取要掃描的包;registerServiceAnnotationBeanPostProcessor方法則是通過BeanDefinitionReaderUtils.registerWithGeneratedName注冊(cè)ServiceAnnotationBeanPostProcessor
registerReferenceAnnotationBeanPostProcessor方法則是通過BeanRegistrar.registerInfrastructureBean注冊(cè)ReferenceAnnotationBeanPostProcessor
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分別驗(yàn)證了providerContext、consumerContext
DubboComponentScanRegistrar實(shí)現(xiàn)了ImportBeanDefinitionRegistrar接口定義的registerBeanDefinitions方法;它首先通過getPackagesToScan方法從AnnotationMetadata讀取要掃描的包,然后再執(zhí)行registerServiceAnnotationBeanPostProcessor、registerReferenceAnnotationBeanPostProcessor方法
getPackagesToScan方法讀取DubboComponentScan注解信息,獲取要掃描的包;registerServiceAnnotationBeanPostProcessor方法則是通過BeanDefinitionReaderUtils.registerWithGeneratedName注冊(cè)ServiceAnnotationBeanPostProcessor
registerReferenceAnnotationBeanPostProcessor方法則是通過BeanRegistrar.registerInfrastructureBean注冊(cè)ReferenceAnnotationBeanPostProcessor
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。