一、Spring 可扩展 Schema
在 dubbo-demo-provider 项目中,我们在如下文件中配置服务提供方:
文件内容为:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl" />
<dubbo:service protocol="rmi" interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" />
</beans>
本节将分析这一部分的实现过程,即如何通过配置文件生成实现类对象;
在很多情况下,我们需要为系统提供可配置化支持,简单的做法可以直接基于 Spring
的标准 Bean 来配置,但配置较为复杂或者需要更多丰富控制的时候,会显得非常笨拙。一
般的做法会用原生态的方式去解析定义好的 xml 文件,然后转化为配置对象,这种方式当然
可以解决所有问题,但实现起来比较繁琐,特别是是在配置非常复杂的时候,解析工作是一
个不得不考虑的负担。 Spring 提供了可扩展 Schema 的支持,这是一个不错的折中方案,完成一个自定义配置一般需要以下步骤:
设计配置属性和 JavaBean
编写 XSD 文件
编写 NamespaceHandler 和 BeanDefinitionParser 完成解析工作
编写 spring.handlers 和 spring.schemas 串联起所有部件
在 Bean 文件中应用
二、Spring读取dubbo标签
Spring框架提供了两个接口:NamespaceHandler负责namespace处理和BeanDefinitionParser负责bean的解析。使用者可以实现NamespaceHandler接口,然后可以在classpath(包括class文件路径和jar包中的路径)的META-INF目录下编写一个spring.handlers文件,该文件中定义名称空间URL和名称空间处理器类的映射,如dubbo框架的spring.handlers文件内容如下:
http\://code.alibabatech.com/schema/dubbo=com.alibaba.dubbo.config.spring.schema.DubboNamespaceHandler
Spring框架初始化时会加载所有classpath的spring.handlers文件,把namespace URL和namespace处理器的映射存到一个Map中,Spring框架在解析bean定义文档时,遇到了非IOC内置(beans名称空间下)的标签,会在这个Map中查找namespace处理器,使用这个自定义的处理器来进行标签解析工作,可以在DefaultBeanDefinitionDocumentReader和BeanDefinitionParserDelegate类中看到相关的代码:
在org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader类下
/** * Parse the elements at the root level in the document: * "import", "alias", "bean". * @param root the DOM root element of the document */
protected void parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate) {
if (delegate.isDefaultNamespace(root)) {
NodeList nl = root.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
if (node instanceof Element) {
Element ele = (Element) node;
if (delegate.isDefaultNamespace(ele)) {
parseDefaultElement(ele, delegate); //解析默认标签
}
else {
delegate.parseCustomElement(ele); //解析自定义标签
}
}
}
}
else {
delegate.parseCustomElement(root);
}
}
Dubbo框架实现了DubboNamespaceHandler 来处理dubbo名称空间:
/* * Copyright 1999-2011 Alibaba Group. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
package com.alibaba.dubbo.config.spring.schema;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
import com.alibaba.dubbo.common.Version;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ConsumerConfig;
import com.alibaba.dubbo.config.ModuleConfig;
import com.alibaba.dubbo.config.MonitorConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.ProviderConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.spring.AnnotationBean;
import com.alibaba.dubbo.config.spring.ReferenceBean;
import com.alibaba.dubbo.config.spring.ServiceBean;
/** * DubboNamespaceHandler * * @author william.liangf * @export */
public class DubboNamespaceHandler extends NamespaceHandlerSupport {
static {
Version.checkDuplicate(DubboNamespaceHandler.class);
}
public void init() {
registerBeanDefinitionParser("application", new DubboBeanDefinitionParser(ApplicationConfig.class, true));
registerBeanDefinitionParser("module", new DubboBeanDefinitionParser(ModuleConfig.class, true));
registerBeanDefinitionParser("registry", new DubboBeanDefinitionParser(RegistryConfig.class, true));
registerBeanDefinitionParser("monitor", new DubboBeanDefinitionParser(MonitorConfig.class, true));
registerBeanDefinitionParser("provider", new DubboBeanDefinitionParser(ProviderConfig.class, true));
registerBeanDefinitionParser("consumer", new DubboBeanDefinitionParser(ConsumerConfig.class, true));
registerBeanDefinitionParser("protocol", new DubboBeanDefinitionParser(ProtocolConfig.class, true));
registerBeanDefinitionParser("service", new DubboBeanDefinitionParser(ServiceBean.class, true));
registerBeanDefinitionParser("reference", new DubboBeanDefinitionParser(ReferenceBean.class, false));
registerBeanDefinitionParser("annotation", new DubboBeanDefinitionParser(AnnotationBean.class, true));
}
}
容器初始化时给dubbo所有标签都注册了一个解析器,其他的标签主要任务是给框架设置一些全局属性可以暂时先放一边,重点需要看下service和reference两个标签,它们分别用来注册服务和服务消费者,dubbo:service会被框架解析成一个ServiceBean,dubbo:reference会被框架解析成一个ReferenceBean。
所有的解析工作都在 DubboBeanDefinitionParser 中:
/** * 解析dubbo自定义标签,往BeanDefinition设置属性值,这个时候bean还没有创建 * @param element * @param parserContext * @param beanClass * @param required * @return */
@SuppressWarnings("unchecked")
private static BeanDefinition parse(Element element, ParserContext parserContext, Class<?> beanClass, boolean required) {
RootBeanDefinition beanDefinition = new RootBeanDefinition();
beanDefinition.setBeanClass(beanClass);
//设置懒加载为false,表示立即加载,spring启动时,立刻进行实例化
//如果设置为true,那么要第一次向容器通过getBean索取bean时实例化,在spring bean的配置里可以配置
//这里会设置懒加载为false其实还可以得到一个推断就是:dubbo标签创建的bean就是单例bean(singleton bean)
//因为lazy-init的设置只对singleton bean有效,对原型bean(prototype无效)
beanDefinition.setLazyInit(false);
String id = element.getAttribute("id");
//如果没有设置bean的id
if ((id == null || id.length() == 0) && required) {
String generatedBeanName = element.getAttribute("name");
//name没有配置
if (generatedBeanName == null || generatedBeanName.length() == 0) {
//如果是ProtocolConfig类型,bean name默认为 dubbo,其他的为配置的interface值
if (ProtocolConfig.class.equals(beanClass)) {
generatedBeanName = "dubbo";
} else {
generatedBeanName = element.getAttribute("interface");
}
}
/* * 如果还为null 那么取 beanClass 的名字,beanClass 其实就是要解析的类型 * 如:com.alibaba.dubbo.config.ApplicationConfig */
if (generatedBeanName == null || generatedBeanName.length() == 0) {
generatedBeanName = beanClass.getName();
}
//如果id没有设置那么 id = generatedBeanName,如果是ProtocolConfig类型的话自然就是 dubbo
id = generatedBeanName;
int counter = 2;
/* * 由于spring的bean id不能重复,但有些标签可能会配置多个如:<dubbo:registry * 所以 id 在后面加数字 2、3、4 区分 */
while(parserContext.getRegistry().containsBeanDefinition(id)) {
id = generatedBeanName + (counter ++);
}
}
if (id != null && id.length() > 0) {
//检查是否有 bean id 相同的
if (parserContext.getRegistry().containsBeanDefinition(id)) {
throw new IllegalStateException("Duplicate spring bean id " + id);
}
/* * 注册 bean 定义 * org.springframework.beans.factory.support.DefaultListableBeanFactory#registerBeanDefinition * 会按照 id 即beanName做一些检查,判断是否重载已加载过的bean等等 * 跟到代码里其实 bean 的注册也是放到 ConcurrentHashMap 里 * beanName也就是这里的 id 会放到 list 里 */
parserContext.getRegistry().registerBeanDefinition(id, beanDefinition);
//给bean添加属性值
beanDefinition.getPropertyValues().addPropertyValue("id", id);
}
if (ProtocolConfig.class.equals(beanClass)) {
for (String name : parserContext.getRegistry().getBeanDefinitionNames()) {
BeanDefinition definition = parserContext.getRegistry().getBeanDefinition(name);
PropertyValue property = definition.getPropertyValues().getPropertyValue("protocol");
if (property != null) {
Object value = property.getValue();
if (value instanceof ProtocolConfig && id.equals(((ProtocolConfig) value).getName())) {
definition.getPropertyValues().addPropertyValue("protocol", new RuntimeBeanReference(id));
}
}
}
} else if (ServiceBean.class.equals(beanClass)) { //解析<dubbo:service
String className = element.getAttribute("class");//获取类全名
if(className != null && className.length() > 0) {
RootBeanDefinition classDefinition = new RootBeanDefinition();
//通过反射获取类
classDefinition.setBeanClass(ReflectUtils.forName(className));
classDefinition.setLazyInit(false);
/* 解析子节点,有些配置可能是: <dubbo:service interface="com.alihealth.dubbo.api.drugInfo.service.DemoService" executes="10" > <property ref="demoService" name="ref"></property> <property value="1.0.0" name="version"></property> </dubbo:service> */
parseProperties(element.getChildNodes(), classDefinition);
/* ref直接设置成了 接口名 + Impl 的bean ? 如:com.alihealth.dubbo.api.drugInfo.service.DemoService + Impl 的bean为啥? 那<dubbo:service里定义的 ref 属性有啥用 */
beanDefinition.getPropertyValues().addPropertyValue("ref", new BeanDefinitionHolder(classDefinition, id + "Impl"));
}
} else if (ProviderConfig.class.equals(beanClass)) {
/* <dubbo:provider 为缺省配置 ,所以在解析的时候,如果<dubbo:service有些值没配置,那么会用<dubbo:provider值进行填充 */
parseNested(element, parserContext, ServiceBean.class, true, "service", "provider", id, beanDefinition);
} else if (ConsumerConfig.class.equals(beanClass)) {
/* * 同上 */
parseNested(element, parserContext, ReferenceBean.class, false, "reference", "consumer", id, beanDefinition);
}
Set<String> props = new HashSet<String>();
ManagedMap parameters = null;
for (Method setter : beanClass.getMethods()) {
String name = setter.getName();
//给model注入值时,如ServiceConfig,方法必须是set开头,并且参数只能为1
if (name.length() > 3 && name.startsWith("set")
&& Modifier.isPublic(setter.getModifiers())
&& setter.getParameterTypes().length == 1) {
//方法参数类型,因为参数只能是1,所以直接取[0]
Class<?> type = setter.getParameterTypes()[0];
//根据set方法名获取属性值,如:setListener 得到的属性为:listener
String property = StringUtils.camelToSplitName(name.substring(3, 4).toLowerCase() + name.substring(4), "-");
props.add(property);
Method getter = null;
try {
getter = beanClass.getMethod("get" + name.substring(3), new Class<?>[0]);
} catch (NoSuchMethodException e) {
try {
getter = beanClass.getMethod("is" + name.substring(3), new Class<?>[0]);
} catch (NoSuchMethodException e2) {
}
}
if (getter == null
|| ! Modifier.isPublic(getter.getModifiers())
|| ! type.equals(getter.getReturnType())) {
continue;
}
if ("parameters".equals(property)) {
/* * 如果属性为 parameters,如ProtocolConfig里的setParameters(Map<String, String> parameters) * 那么去子节点获取 <dubbo:parameter * <dubbo:protocol name="dubbo" host="127.0.0.1" port="9998" accepts="1000" > <dubbo:parameter key="adsf" value="adf" /> <dubbo:parameter key="errer" value="aerdf" /> </dubbo:protocol> */
parameters = parseParameters(element.getChildNodes(), beanDefinition);
} else if ("methods".equals(property)) {
/* 解析 <dubbo:method 并设置 methods 值 --serviceConfig中 */
parseMethods(id, element.getChildNodes(), beanDefinition, parserContext);
} else if ("arguments".equals(property)) {
/* 同上 ,解析<dubbo:argument --- MethodConfig中 */
parseArguments(id, element.getChildNodes(), beanDefinition, parserContext);
} else {
String value = element.getAttribute(property);
if (value != null) {
value = value.trim();
if (value.length() > 0) {
//不发布到任何注册中心时 registry = "N/A"
if ("registry".equals(property) && RegistryConfig.NO_AVAILABLE.equalsIgnoreCase(value)) {
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setAddress(RegistryConfig.NO_AVAILABLE);
beanDefinition.getPropertyValues().addPropertyValue(property, registryConfig);
} else if ("registry".equals(property) && value.indexOf(',') != -1) {
//多注册中心用 , 号分隔
parseMultiRef("registries", value, beanDefinition, parserContext);
} else if ("provider".equals(property) && value.indexOf(',') != -1) {
parseMultiRef("providers", value, beanDefinition, parserContext);
} else if ("protocol".equals(property) && value.indexOf(',') != -1) {
//同上 多协议暴露
parseMultiRef("protocols", value, beanDefinition, parserContext);
} else {
Object reference;
if (isPrimitive(type)) {//如果参数类型为 java 的基本类型
if ("async".equals(property) && "false".equals(value)
|| "timeout".equals(property) && "0".equals(value)
|| "delay".equals(property) && "0".equals(value)
|| "version".equals(property) && "0.0.0".equals(value)
|| "stat".equals(property) && "-1".equals(value)
|| "reliable".equals(property) && "false".equals(value)) {
/* 兼容旧版本xsd中的default值,以上配置的值在xsd中有配置defalt值 <xsd:attribute name="version" type="xsd:string" use="optional" default="0.0.0"> */
value = null;
}
reference = value;
} else if ("protocol".equals(property)
//如果属性为 protocol 那么要判断protocol对应的拓展点配置有没有
&& ExtensionLoader.getExtensionLoader(Protocol.class).hasExtension(value)
//检查当前使用的协议是否已经解析过 可能在这里被解析过<dubbo:protocol name="dubbo"
&& (! parserContext.getRegistry().containsBeanDefinition(value)
|| ! ProtocolConfig.class.getName().equals(parserContext.getRegistry().getBeanDefinition(value).getBeanClassName()))) {
if ("dubbo:provider".equals(element.getTagName())) {
logger.warn("Recommended replace <dubbo:provider protocol=\"" + value + "\" ... /> to <dubbo:protocol name=\"" + value + "\" ... />");
}
// 兼容旧版本配置
ProtocolConfig protocol = new ProtocolConfig();
protocol.setName(value);
reference = protocol;
} else if ("monitor".equals(property)
//同上
&& (! parserContext.getRegistry().containsBeanDefinition(value)
|| ! MonitorConfig.class.getName().equals(parserContext.getRegistry().getBeanDefinition(value).getBeanClassName()))) {
// 兼容旧版本配置
reference = convertMonitor(value);
} else if ("onreturn".equals(property)) {
//回调方法 类似onSuccess
int index = value.lastIndexOf(".");
// bean的名字
String returnRef = value.substring(0, index);
String returnMethod = value.substring(index + 1);
reference = new RuntimeBeanReference(returnRef);
beanDefinition.getPropertyValues().addPropertyValue("onreturnMethod", returnMethod);
} else if ("onthrow".equals(property)) {
//回调 异常执行的方法 ,类似 onError
int index = value.lastIndexOf(".");
String throwRef = value.substring(0, index);
String throwMethod = value.substring(index + 1);
reference = new RuntimeBeanReference(throwRef);
beanDefinition.getPropertyValues().addPropertyValue("onthrowMethod", throwMethod);
} else {
if ("ref".equals(property) && parserContext.getRegistry().containsBeanDefinition(value)) {
BeanDefinition refBean = parserContext.getRegistry().getBeanDefinition(value);
/* 必须是单例bean(singleton),原型bean(prototype)不行,sevice初始化一次,在spring容器里也只有一个 实例 是不是和dubbo的幂等有关,如果为原型bean,那么服务就变成有状态的了 */
if (! refBean.isSingleton()) {
throw new IllegalStateException("The exported service ref " + value + " must be singleton! Please set the " + value + " bean scope to singleton, eg: <bean id=\"" + value+ "\" scope=\"singleton\" ...>");
}
}
reference = new RuntimeBeanReference(value);
}
/* 设置属性,值为另外一个关联的bean RuntimeBeanReference 固定占位符类,当在beanfactory中作为另外一个bean的引用时,作为属性值对象,将在运行时进行解析 */
beanDefinition.getPropertyValues().addPropertyValue(property, reference);
}
}
}
}
}
}
NamedNodeMap attributes = element.getAttributes();
int len = attributes.getLength();
for (int i = 0; i < len; i++) {
Node node = attributes.item(i);
String name = node.getLocalName();
//经过上面的解析,如果还有一些属性没有解析到的
if (! props.contains(name)) {
if (parameters == null) {
parameters = new ManagedMap();
}
String value = node.getNodeValue();
parameters.put(name, new TypedStringValue(value, String.class));
}
}
if (parameters != null) {
beanDefinition.getPropertyValues().addPropertyValue("parameters", parameters);
}
return beanDefinition;
}
@SuppressWarnings("unchecked")
private static void parseMultiRef(String property, String value, RootBeanDefinition beanDefinition,
ParserContext parserContext) {
//解析 registries 、providers、protocols 时支持多引用
String[] values = value.split("\\s*[,]+\\s*");
ManagedList list = null;
for (int i = 0; i < values.length; i++) {
String v = values[i];
if (v != null && v.length() > 0) {
if (list == null) {
list = new ManagedList();
}
list.add(new RuntimeBeanReference(v));
}
}
beanDefinition.getPropertyValues().addPropertyValue(property, list);
}
private static void parseNested(Element element, ParserContext parserContext, Class<?> beanClass,
boolean required, String tag, String property, String ref, BeanDefinition beanDefinition) {
NodeList nodeList = element.getChildNodes();
if (nodeList != null && nodeList.getLength() > 0) {
boolean first = true;
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node instanceof Element) {
if (tag.equals(node.getNodeName())
|| tag.equals(node.getLocalName())) {
if (first) {
first = false;
String isDefault = element.getAttribute("default");
/*
如果 <dubbo:provider 标签没有配置default开关,那么直接设置 default = "false"
这样做的目的是为了让 <dubbo:provider里的配置都只是 <dubbo:service 或 <dubbo:reference的默认或缺省配置
*/
if (isDefault == null || isDefault.length() == 0) {
beanDefinition.getPropertyValues().addPropertyValue("default", "false");
}
}
BeanDefinition subDefinition = parse((Element) node, parserContext, beanClass, required);
if (subDefinition != null && ref != null && ref.length() > 0) {
subDefinition.getPropertyValues().addPropertyValue(property, new RuntimeBeanReference(ref));
}
}
}
}
}
}
private static void parseProperties(NodeList nodeList, RootBeanDefinition beanDefinition) {
if (nodeList != null && nodeList.getLength() > 0) {
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node instanceof Element) {
//如果是 <property 元素
if ("property".equals(node.getNodeName())
|| "property".equals(node.getLocalName())) {
String name = ((Element) node).getAttribute("name");
if (name != null && name.length() > 0) {
String value = ((Element) node).getAttribute("value");
//获取 ref
String ref = ((Element) node).getAttribute("ref");
if (value != null && value.length() > 0) {
beanDefinition.getPropertyValues().addPropertyValue(name, value);
} else if (ref != null && ref.length() > 0) {
beanDefinition.getPropertyValues().addPropertyValue(name, new RuntimeBeanReference(ref));
} else {
/* 只支持两种property的设置方法: <property ref="" name=""> <property value="" name=""> */
throw new UnsupportedOperationException("Unsupported <property name=\"" + name + "\"> sub tag, Only supported <property name=\"" + name + "\" ref=\"...\" /> or <property name=\"" + name + "\" value=\"...\" />");
}
}
}
}
}
}
}
@SuppressWarnings("unchecked")
private static ManagedMap parseParameters(NodeList nodeList, RootBeanDefinition beanDefinition) {
if (nodeList != null && nodeList.getLength() > 0) {
ManagedMap parameters = null;
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node instanceof Element) {
//解析 <dubbo:parameter
if ("parameter".equals(node.getNodeName())
|| "parameter".equals(node.getLocalName())) {
if (parameters == null) {
parameters = new ManagedMap();
}
String key = ((Element) node).getAttribute("key");
String value = ((Element) node).getAttribute("value");
boolean hide = "true".equals(((Element) node).getAttribute("hide"));
if (hide) {
key = Constants.HIDE_KEY_PREFIX + key;
}
//添加参数,String 类型
parameters.put(key, new TypedStringValue(value, String.class));
}
}
}
return parameters;
}
return null;
}
@SuppressWarnings("unchecked")
private static void parseMethods(String id, NodeList nodeList, RootBeanDefinition beanDefinition,
ParserContext parserContext) {
if (nodeList != null && nodeList.getLength() > 0) {
ManagedList methods = null;
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node instanceof Element) {
Element element = (Element) node;
//<dubbo:method
if ("method".equals(node.getNodeName()) || "method".equals(node.getLocalName())) {
String methodName = element.getAttribute("name");
if (methodName == null || methodName.length() == 0) {
throw new IllegalStateException("<dubbo:method> name attribute == null");
}
if (methods == null) {
methods = new ManagedList();
}
//解析 <dubbo:method MethodConfig
BeanDefinition methodBeanDefinition = parse(((Element) node),
parserContext, MethodConfig.class, false);
String name = id + "." + methodName;
BeanDefinitionHolder methodBeanDefinitionHolder = new BeanDefinitionHolder(
methodBeanDefinition, name);
methods.add(methodBeanDefinitionHolder);
}
}
}
if (methods != null) {
beanDefinition.getPropertyValues().addPropertyValue("methods", methods);
}
}
}
解析的最终目的是返回 RootBeanDefinition 对象,RootBeanDefinition包含了解析出来的关于bean的所有信息,注意,在bean的解析完后其实只是spring将其转化成spring内部的一种抽象的数据对象结构,bean的创建(实例化)是第一次调用 getBean 时创建的。