Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the function of DubboComponentScanRegistrar in dubbo

Shulou Source: shulou.com Published: 2022-06-02 10:15:15 10月01日 Update

What is the role of DubboComponentScanRegistrar in dubbo, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

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 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 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 @ ReferenceAnnotationBean Processor BeanRegistrar.registerInfrastructureBean (registry, ReferenceAnnotationBeanPostProcessor.BEAN_NAME, ReferenceAnnotationBeanPostProcessor.class) } private Set 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 packagesToScan = new LinkedHashSet (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 implements the registerBeanDefinitions method defined by the ImportBeanDefinitionRegistrar interface; it first reads the packet to be scanned from AnnotationMetadata through the getPackagesToScan method, and then executes the registerServiceAnnotationBeanPostProcessor and registerReferenceAnnotationBeanPostProcessor methods

The getPackagesToScan method reads the DubboComponentScan comment information and gets the package to be scanned, while the registerServiceAnnotationBeanPostProcessor method registers the ServiceAnnotationBeanPostProcessor through BeanDefinitionReaderUtils.registerWithGeneratedName

The registerReferenceAnnotationBeanPostProcessor method registers the ReferenceAnnotationBeanPostProcessor through BeanRegistrar.registerInfrastructureBean

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); / / FromChild demoService = child.getDemoServiceFromChild (); Assertions.assertNotNull (demoService) Value = demoService.sayName ("Mercy"); Assertions.assertEquals ("Hello,Mercy", value); / / FromParent demoService = child.getDemoServiceFromParent (); Assertions.assertNotNull (demoService); value = demoService.sayName ("Mercy"); Assertions.assertEquals ("Hello,Mercy", value); / / FromAncestor demoService = child.getDemoServiceFromAncestor (); Assertions.assertNotNull (demoService) Value = demoService.sayName ("Mercy"); Assertions.assertEquals ("Hello,Mercy", value); providerContext.close (); consumerContext.close ();}}

DubboComponentScanRegistrarTest verifies providerContext and consumerContext respectively.

Summary

DubboComponentScanRegistrar implements the registerBeanDefinitions method defined by the ImportBeanDefinitionRegistrar interface; it first reads the packet to be scanned from AnnotationMetadata through the getPackagesToScan method, and then executes the registerServiceAnnotationBeanPostProcessor and registerReferenceAnnotationBeanPostProcessor methods

The getPackagesToScan method reads the DubboComponentScan comment information and gets the package to be scanned, while the registerServiceAnnotationBeanPostProcessor method registers the ServiceAnnotationBeanPostProcessor through BeanDefinitionReaderUtils.registerWithGeneratedName

The registerReferenceAnnotationBeanPostProcessor method registers the ReferenceAnnotationBeanPostProcessor through BeanRegistrar.registerInfrastructureBean

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Tags: Method information interface annotation help function clarity content this summary article novice more knowledge industry information information channel problem demand channel Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Microsoft Shulou Tech Info Apple Docker