Analysis of Dubbo's ServiceBean mechanism

ServiceBean Analysis

ServiceBean<T> This class 
(1) inherits ServiceConfig<T>: This class mainly encapsulates the attributes of tags, such as ref, path, methods, etc.; 
(2) Implements the InitializingBean and DisposableBean interfaces, which are as follows: About initialization in the spring container There are three ways to define the bean and the operations performed before destruction: 
the first one: through the @PostConstruct and @PreDestroy methods to implement the operations performed before initialization and destruction of the bean 
The second one is: by defining init-method and destroy-method in xml The third method 
is: Implement InitializingBean and DisposableBean interfaces through beans

(3) Implement the ApplicationContextAware interface: To 
implement this interface, you need to override the setApplicationContext(ApplicationContext applicationContext) method. 
Function: You can get the ApplicationContext. We know that the most important thing in Spring is the ApplicationContext. After setting the ApplicationContext, you can use the beans or other objects in the container at will.

(4) Implement the ApplicationListener interface: To 
implement this interface, you need to override the onApplicationEvent(ApplicationEvent event) method. The effect is as follows:

You can listen to the events published through applicationContext.publistEvent (ApplicationEvent event)). During the startup process of Spring, after the initialization of the object is completed, the publistEvent method will be called to publish the event, and then all interfaces that implement ApplicationListener can receive the event and execute it. Response action.

For details, please refer to: 
http://wiki.jikexueyuan.com/project/spring/event-handling-in-spring.html  
http://www.cnblogs.com/ArtsCrafts/p/Spring_Event.html

Here, the service is exposed by calling the export() method by listening to Spring's built-in event ContextRefreshedEvent.

(5) Implement the BeanNameAware interface: To 
implement this interface, you need to override the setBeanName(String name) method. Function: Let the bean get its own name in the BeanFactory configuration (id or name according to the situation), which is suitable for a bean that needs to access itself in the configuration file. The case of the id property of the bean.

    That is to say, when parsing the service tag, the beanName needs to be defined, and the obtained beanName is used in the afterPropertiesSet method in the setPath. 
Specific reference: 
http://langgufu.iteye.com/blog/1499966  
http://www.cnblogs.com/liunanjava/p/4401089.html

Description: 
It is found by running dubbo-demo-provider that the beanName given here is: com.alibaba.dubbo.demo.DemoService. In theory, it should be the set id, so let's look at the configuration file:

<bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl" />

<dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" />

    We found that there is no id or name attribute specified for the <dubbo:service> tag, so how is this beanName set?

Looking at the parsing code we can see a segment like this:

String id = element.getAttribute("id");
        if ((id == null || id.length() == 0) && required) {
            String generatedBeanName = element.getAttribute("name");
            if (generatedBeanName == null || generatedBeanName.length() == 0) {
                if (ProtocolConfig.class.equals(beanClass)) {
                    generatedBeanName = "dubbo";
                } else {
                    generatedBeanName = element.getAttribute("interface");
                }
            }
            if (generatedBeanName == null || generatedBeanName.length() == 0) {
                generatedBeanName = beanClass.getName();
            }
            id = generatedBeanName; 
            int counter = 2;
            while(parserContext.getRegistry().containsBeanDefinition(id)) {
                id = generatedBeanName + (counter ++);
            }
        }
        if (id != null && id.length() > 0) {
            if (parserContext.getRegistry().containsBeanDefinition(id))  {
                throw new IllegalStateException("Duplicate spring bean id " + id);
            }
            parserContext.getRegistry().registerBeanDefinition(id, beanDefinition);
            beanDefinition.getPropertyValues().addPropertyValue("id", id);
        }
        ...

    It is very clear here, the reason is that if the id attribute is not set, the interface attribute will be taken as the id of the SpringBean.

The other content is some settings providerConfig, applicationConfig, moduleConfig, registryConfigs, monitorConfig, protocolConfigs

For details, please refer to: 
http://chenjingbo.iteye.com/blog/2008325

References: http://blog.csdn.net/xiaoxiaoxuanao/article/details/54691641

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325185715&siteId=291194637