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

The specific usage of DubboApplicationContextInitializer in dubbo

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces "the specific usage of DubboApplicationContextInitializer in dubbo". In daily operation, I believe many people have doubts about the specific usage of DubboApplicationContextInitializer in dubbo. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "the specific usage of DubboApplicationContextInitializer in dubbo"! Next, please follow the editor to study!

DubboApplicationContextInitializer

Dubbo-spring-boot-project-2.7.3/dubbo-spring-boot-compatible/autoconfigure/src/main/java/org/apache/dubbo/spring/boot/context/DubboApplicationContextInitializer.java

Public class DubboApplicationContextInitializer implements ApplicationContextInitializer, Ordered {@ Override public void initialize (ConfigurableApplicationContext applicationContext) {overrideBeanDefinitions (applicationContext);} private void overrideBeanDefinitions (ConfigurableApplicationContext applicationContext) {applicationContext.addBeanFactoryPostProcessor (new OverrideBeanDefinitionRegistryPostProcessor ()); applicationContext.addBeanFactoryPostProcessor (new DubboConfigBeanDefinitionConflictProcessor ());} @ Override public int getOrder () {return HIGHEST_PRECEDENCE;}}

DubboApplicationContextInitializer implements ApplicationContextInitializer, Ordered interface, and the initialize method executes overrideBeanDefinitions. It adds OverrideBeanDefinitionRegistryPostProcessor to applicationContext, and DubboConfigBeanDefinitionConflictProcessor;getOrder returns HIGHEST_PRECEDENCE.

OverrideBeanDefinitionRegistryPostProcessor

Dubbo-spring-boot-project-2.7.3/dubbo-spring-boot-compatible/autoconfigure/src/main/java/org/apache/dubbo/spring/boot/beans/factory/config/OverrideBeanDefinitionRegistryPostProcessor.java

Public class OverrideBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {@ Override public void postProcessBeanDefinitionRegistry (BeanDefinitionRegistry registry) throws BeansException {registerInfrastructureBean (registry, BEAN_NAME, DubboConfigBeanCustomizer.class);} @ Override public void postProcessBeanFactory (ConfigurableListableBeanFactory beanFactory) throws BeansException {}}

OverrideBeanDefinitionRegistryPostProcessor implements the BeanDefinitionRegistryPostProcessor interface, and its postProcessBeanDefinitionRegistry method calls the BeanRegistrar.registerInfrastructureBean method to register DubboConfigBeanCustomizer

BeanRegistrar

Dubboth2.7.3Mutual sources.jarpickplicorgUnixapacheanddubboqqqqingLigandutillyBeanRegistrar.java

Public class BeanRegistrar {/ * Register InfrastructureBean * * @ param beanDefinitionRegistry {@ link BeanDefinitionRegistry} * @ param beanType the type of bean * @ param beanName the name of bean * / public static void registerInfrastructureBean (BeanDefinitionRegistry beanDefinitionRegistry, String beanName Class beanType) {if (! beanDefinitionRegistry.containsBeanDefinition (beanName)) {RootBeanDefinition beanDefinition = new RootBeanDefinition (beanType) BeanDefinition.setRole (BeanDefinition.ROLE_INFRASTRUCTURE); beanDefinitionRegistry.registerBeanDefinition (beanName, beanDefinition);}

RegisterInfrastructureBean registers BeanDefinition.ROLE_INFRASTRUCTURE 's bean if beanDefinitionRegistry does not have the beanDefinition.

DubboConfigBeanCustomizer

Dubbo-spring-boot-project-2.7.3/dubbo-spring-boot-compatible/autoconfigure/src/main/java/org/apache/dubbo/spring/boot/beans/factory/config/DubboConfigBeanCustomizer.java

@ Deprecatedclass DubboConfigBeanCustomizer extends NamePropertyDefaultValueDubboConfigBeanCustomizer {@ Override public void customize (String beanName, AbstractConfig dubboConfigBean) {boolean valid = isValidPropertyName (dubboConfigBean, beanName); if (valid) {super.customize (beanName, dubboConfigBean);}} private boolean isValidPropertyName (AbstractConfig dubboConfigBean, String propertyValue) {boolean valid = true; String propertyName = "name" / AbstractConfig.checkName (String,String) Method method = findMethod (AbstractConfig.class, "checkName", String.class, String.class); try {if (! method.isAccessible ()) {method.setAccessible (true);} if (BeanUtils.getPropertyDescriptor (dubboConfigBean.getClass (), propertyName)! = null) {invokeMethod (method, null, propertyName, propertyValue) } catch (IllegalStateException e) {valid = false;} return valid;}}

DubboConfigBeanCustomizer inherits NamePropertyDefaultValueDubboConfigBeanCustomizer, and its customize method verifies propertyName first, and executes the customize method of the parent class only in the case of valid.

DubboConfigBeanDefinitionConflictProcessor

Dubbo-spring-boot-project-2.7.3/dubbo-spring-boot-compatible/autoconfigure/src/main/java/org/apache/dubbo/spring/boot/beans/factory/config/DubboConfigBeanDefinitionConflictProcessor.java

Public class DubboConfigBeanDefinitionConflictProcessor implements BeanDefinitionRegistryPostProcessor, Ordered {private final Logger logger = LoggerFactory.getLogger (getClass ()); private BeanDefinitionRegistry registry; private Environment environment; @ Override public void postProcessBeanDefinitionRegistry (BeanDefinitionRegistry registry) throws BeansException {this.registry = registry;} @ Override public void postProcessBeanFactory (ConfigurableListableBeanFactory beanFactory) throws BeansException {resolveUniqueApplicationConfigBean (registry, beanFactory) } / * * Resolve the unique {@ link ApplicationConfig} Bean * * @ param registry {@ link BeanDefinitionRegistry} instance * @ param beanFactory {@ link ConfigurableListableBeanFactory} instance * @ see EnableDubboConfig * / private void resolveUniqueApplicationConfigBean (BeanDefinitionRegistry registry, ConfigurableListableBeanFactory beanFactory) {this.environment = beanFactory.getBean (ENVIRONMENT_BEAN_NAME, Environment.class) String [] beansNames = beanNamesForTypeIncludingAncestors (beanFactory, ApplicationConfig.class); if (beansNames.length

< 2) { // If the number of ApplicationConfig beans is less than two, return immediately. return; } // Remove ApplicationConfig Beans that are configured by "dubbo.application.*" Stream.of(beansNames) .filter(this::isConfiguredApplicationConfigBeanName) .forEach(registry::removeBeanDefinition); beansNames = beanNamesForTypeIncludingAncestors(beanFactory, ApplicationConfig.class); if (beansNames.length >

1) {throw new IllegalStateException (String.format ("There are more than one instances of% s, whose bean definitions:% s", ApplicationConfig.class.getSimpleName (), Stream.of (beansNames) .map (registry::getBeanDefinition) .targets (Collectors.toList () }} private boolean isConfiguredApplicationConfigBeanName (String beanName) {boolean removed = BeanFactoryUtils.isGeneratedBeanName (beanName) / / Dubbo ApplicationConfig id as bean name | | Objects.equals (beanName, environment.getProperty ("dubbo.application.id")) If (removed) {if (logger.isWarnEnabled ()) {logger.warn ("The {} bean [name: {}] has been removed!", ApplicationConfig.class.getSimpleName (), beanName);} return removed;} @ Override public int getOrder () {return LOWEST_PRECEDENCE;}}

DubboConfigBeanDefinitionConflictProcessor implements BeanDefinitionRegistryPostProcessor and Ordered interface; the postProcessBeanFactory method executes resolveUniqueApplicationConfigBean to verify whether there are multiple instances of the same beanDefinition, and if so, it throws IllegalStateException;getOrder and returns LOWEST_PRECEDENCE.

Summary

DubboApplicationContextInitializer implements ApplicationContextInitializer, Ordered interface, and the initialize method executes overrideBeanDefinitions. It adds OverrideBeanDefinitionRegistryPostProcessor to applicationContext, and DubboConfigBeanDefinitionConflictProcessor;getOrder returns HIGHEST_PRECEDENCE.

At this point, the study of "the specific use of DubboApplicationContextInitializer in dubbo" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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