Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the function of DubboComponentScanRegistrar in dubbo

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report