1.发现问题
1.1 由dubbo快速入门案例之服务端引发的思考
provider.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="hello-world-app" />
<!-- 使用zookeeper广播注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.doubeier.dubbo.common.infterface.DemoService" ref="demoService" />
<!-- 和本地bean一样实现服务 -->
<bean id="demoService" class="com.doubeier.dubbo.provider.impl.DemoServiceImpl" />
</beans>
入门启动
public class ProviderDemo {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
context.start();
System.in.read(); // 输入任意键后退出
context.close();
}
}
这里我们发现除了dubbo配置文件中使用了一些以dubbo:开头的标签,这些标签是怎么解析的呢?
此外进行debug的时候还发现,在refresh方法执行完obtainFreshBeanFactory步骤之后,下图:
容器中已经有了dubbo相关的bean的BeanDefinition,这又是什么情况呢?
1.2 问题总结
1.spring如何解析dubbo标签的
2.dubbo相关的bean的BeanDefinition是如何加载的
2.带着问题追溯源码
从obtainFreshBeanFactory方法开始到xml解析
protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
refreshBeanFactory();//从这个方法进去
return getBeanFactory();
}
到AbstractRefreshableApplicationContext类中继续看refreshBeanFactory方法
protected final void refreshBeanFactory() throws BeansException {
if (hasBeanFactory()) {
destroyBeans();
closeBeanFactory();
}
try {
DefaultListableBeanFactory beanFactory = createBeanFactory();
beanFactory.setSerializationId(getId());
customizeBeanFactory(beanFactory);
loadBeanDefinitions(beanFactory); //这个方法进行加载BeanDefinitions
this.beanFactory = beanFactory;
}
catch (IOException ex) {
throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
}
}
到AbstractXmlApplicationContext类中继续看loadBeanDefinitions方法
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
// Create a new XmlBeanDefinitionReader for the given BeanFactory.
XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
// Configure the bean definition reader with this context's
// resource loading environment.
beanDefinitionReader.setEnvironment(this.getEnvironment());
beanDefinitionReader.setResourceLoader(this);
beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));
// Allow a subclass to provide custom initialization of the reader,
// then proceed with actually loading the bean definitions.
initBeanDefinitionReader(beanDefinitionReader);
loadBeanDefinitions(beanDefinitionReader); //从这里继续
}
/** 同一个类中实现的该方法 */
protected void loadBeanDefinitions(XmlBeanDefinitionRead