spring读取xml文件:
- AbstractBeanDefinitionReader读取location下的资源文件
- AbstractBeanDefinitionReader.loadBeanDefinitions(String location, Set actualResources)
- AbstractBeanDefinitionReader.loadBeanDefinitions(Resource… resources)
- XmlBeanDefinitionReader解析资源文件到流,并解析到Document对象
- BeanDefinitionReader.loadBeanDefinitions(Resource resource)
- XmlBeanDefinitionReader.loadBeanDefinitions(Resource resource)
- XmlBeanDefinitionReader.loadBeanDefinitions(EncodedResource encodedResource)
- XmlBeanDefinitionReader.doLoadBeanDefinitions(InputSource inputSource, Resource resource)
- XmlBeanDefinitionReader.registerBeanDefinitions(Document doc, Resource resource)
- DefaultBeanDefinitionDocumentReader获取Document对象的根节点root,并遍历根节点下所有子节点生成Bean
- BeanDefinitionDocumentReader.registerBeanDefinitions(Document doc, Resource resource)
- DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(Document doc, Resource resource)
- DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(Element root)
- DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate)
- 调用BeanDefinitionParserDelegate代理解析xml的root节点
- BeanDefinitionParserDelegate.parseCustomElement(Element ele)
- BeanDefinitionParserDelegate.parseCustomElement(Element ele, BeanDefinition containingBd)
- DubboNamespaceHandler.init()对不同的标签生成不同的DubboBeanDefinitionParser实例
- NamespaceHandler.parse(Element element, ParserContext parserContext)
- NamespaceHandlerSupport.parse(Element element, ParserContext parserContext)
- BeanDefinitionParser.parse(Element element, ParserContext parserContext)
- Dubbo实现BeanDefinitionParser接口对Element进行解析
- DubboBeanDefinitionParser.parse(Element element, ParserContext parserContext)
Dubbo的实现流程:
- 为该节点生成唯一的id
- 取出xml文件属性,设置到xxxConfig对象中
// DubboBeanDefinitionParser.parse(Element element, ParserContext parserContext)
private static BeanDefinition parse(Element element, ParserContext parserContext, Class<?> beanClass, boolean required) {
RootBeanDefinition beanDefinition = new RootBeanDefinition();
beanDefinition.setBeanClass(beanClass);
beanDefinition.setLazyInit(false);
// 获取节点的id属性值
String id = element.getAttribute("id");
if ((id == null || id.length() == 0) && required) { // id为空情况
// 生成一个beanName
// 获取节点的name属性值
String generatedBeanName = element.getAttribute("name");
if (generatedBeanName == null || generatedBeanName.length() == 0) { // name属性值为空
if (ProtocolConfig.class.equals(beanClass)) { // 标签<dubbo:protocol />
// 默认协议为dubbo
generatedBeanName = "dubbo";
} else {
// 其他取interface属性值
generatedBeanName = element.getAttribute("interface");
}
}
if (generatedBeanName == null || generatedBeanName.length() == 0) {
generatedBeanName = beanClass.getName();
}
// 赋值id
id = generatedBeanName;
int counter = 2;
while (parserContext.getRegistry().containsBeanDefinition(id)) {
// 该id已注册,再加上一个计数器
id = generatedBeanName + (counter++);
}
}
if (id != null && id.length() > 0) {
// 该id还是已注册,报异常吧
if (parserContext.getRegistry().containsBeanDefinition(id)) {
throw new IllegalStateException("Duplicate spring bean id " + id);
}
// 注册这个id到BeanDefinitionRegistry(spring对象)中
parserContext.getRegistry().registerBeanDefinition(id, beanDefinition);
// 新增id属性到RootBeanDefinition对象(spring对象)
beanDefinition.getPropertyValues().addPropertyValue("id", id);
}
if (ProtocolConfig.class.equals(beanClass)) { // 处理<dubbo:protocol>
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();
// TODO 这里什么时候会进来呢
if (value instanceof ProtocolConfig && id.equals(((ProtocolConfig) value).getName())) {
definition.getPropertyValues().addPropertyValue("protocol", new RuntimeBeanReference(id));
}
}
}
} else if (ServiceBean.class.equals(beanClass)) { // 处理<dubbo:service>
// 获取节点class属性值
String className = element.getAttribute("class");
if (className != null && className.length() > 0) {
RootBeanDefinition classDefinition = new RootBeanDefinition();
classDefinition.setBeanClass(ReflectUtils.forName(className));
classDefinition.setLazyInit(false);
parseProperties(element.getChildNodes(), classDefinition);
// 添加ref属性到beanDefinition中
beanDefinition.getPropertyValues().addPropertyValue("ref", new BeanDefinitionHolder(classDefinition, id + "Impl"));
}
} else if (ProviderConfig.class.equals(beanClass)) { // 处理<dubbo:provider>
// 有子节点的,解析子节点数据
parseNested(element, parserContext, ServiceBean.class, true, "service", "provider", id, beanDefinition);
} else if (ConsumerConfig.class.equals(beanClass)) { // 处理<dubbo:consumer>
// 有子节点的,解析子节点数据
parseNested(element, parserContext, ReferenceBean.class, false, "reference", "consumer", id, beanDefinition);
}
Set<String> props = new HashSet<String>();
ManagedMap parameters = null;
// 遍历所有的public方法
for (Method setter : beanClass.getMethods()) {
String name = setter.getName();
// 只取setter()方法
if (name.length() > 3 && name.startsWith("set")
&& Modifier.isPublic(setter.getModifiers())
&& setter.getParameterTypes().length == 1) {
// 获取setter()方法第一个参数
Class<?> type = setter.getParameterTypes()[0];
// 获取该setter()设置的属性名
String property = StringUtils.camelToSplitName(name.substring(3, 4).toLowerCase() + name.substring(4), "-");
props.add(property);
Method getter = null;
try {
// 获取getter()方法
getter = beanClass.getMethod("get" + name.substring(3), new Class<?>[0]);
} catch (NoSuchMethodException e) {
try {
// 兼容boolean类型
getter = beanClass.getMethod("is" + name.substring(3), new Class<?>[0]);
} catch (NoSuchMethodException e2) {
}
}
// getter()方法为空 || 该方法不是public || 返回类型不等于setter()方法入参类型
if (getter == null
|| !Modifier.isPublic(getter.getModifiers())
|| !type.equals(getter.getReturnType())) {
continue;
}
// 特殊处理
if ("parameters".equals(property)) {
parameters = parseParameters(element.getChildNodes(), beanDefinition);
} else if ("methods".equals(property)) {
parseMethods(id, element.getChildNodes(), beanDefinition, parserContext);
} else if ("arguments".equals(property)) {
parseArguments(id, element.getChildNodes(), beanDefinition, parserContext);
} else {
String value = element.getAttribute(property);
if (value != null) {
value = value.trim();
if (value.length() > 0) {
// 特殊处理
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)) { // 基本类型
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)) {
// backward compatibility for the default value in old version's xsd
value = null;
}
// 赋值给reference
reference = value;
} else if ("protocol".equals(property)
&& ExtensionLoader.getExtensionLoader(Protocol.class).hasExtension(value)
&& (!parserContext.getRegistry().containsBeanDefinition(value)
|| !ProtocolConfig.class.getName().equals(parserContext.getRegistry().getBeanDefinition(value).getBeanClassName()))) {
// 不推荐的方式<dubbo:provider protocol="dubbo" />
if ("dubbo:provider".equals(element.getTagName())) {
logger.warn("Recommended replace <dubbo:provider protocol=\"" + value + "\" ... /> to <dubbo:protocol name=\"" + value + "\" ... />");
}
// backward compatibility
ProtocolConfig protocol = new ProtocolConfig();
protocol.setName(value);
reference = protocol;
} else if ("onreturn".equals(property)) {
int index = value.lastIndexOf(".");
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)) {
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 ("oninvoke".equals(property)) {
int index = value.lastIndexOf(".");
String invokeRef = value.substring(0, index);
String invokeRefMethod = value.substring(index + 1);
reference = new RuntimeBeanReference(invokeRef);
beanDefinition.getPropertyValues().addPropertyValue("oninvokeMethod", invokeRefMethod);
} else {
// <dubbo:service interface="com.xxx" ref="xxx"/>
if ("ref".equals(property) && parserContext.getRegistry().containsBeanDefinition(value)) {
BeanDefinition refBean = parserContext.getRegistry().getBeanDefinition(value);
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);
}
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();
// xml文件有的属性,但是在xxxConfig中没有Setter、Getter方法的属性
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) {
// 添加到parameters中
beanDefinition.getPropertyValues().addPropertyValue("parameters", parameters);
}
return beanDefinition;
}