3. Xml中默认标签的Bean配置详解
在XML配置Bean的时候,可以使用默认<bean />
标签配置Bean,也可以使用自定义标签如<myname:user />
配置Bean。Spring内部会通过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:myname="http://www.yanglh.com/schema/user"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.yanglh.com/schema/user http://www.yanglh.com/schema/user.xsd">
<myname:user id="testBean" email="yanglh@acca.com.cn" userName="yangliheui"/>
</beans>
xmlns="http://www.springframework.org/schema/beans"默认标签命名空间
xmlns:myname="http://www.yanglh.com/schema/user"自定义标签命名空间
//源码位置:org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader#parseBeanDefinitions
/**
* 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;
// 在这里做了判断,如果命名空间未定义或值为http://www.springframework.org/schema/beans则走默认解析流程
if (delegate.isDefaultNamespace(ele)) {
parseDefaultElement(ele, delegate);
} else {
// 否则走自定义标签的解析流程
delegate.parseCustomElement(ele);
}
}
}
}
else {
delegate.parseCustomElement(root);
}
}
这一篇我们只关注默认标签的Bean配置解析与注册
在
Spring XML默认标签配置解析注册Bean源码分析(一)——概述中我们调用Spring实现了Bean的解析与注册,那么我们就以此为入口正式进入Spring的世界。
Resource resource = new ClassPathResource("conf/beans.xml");
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
reader.loadBeanDefinitions(resource);
Person person = factory.getBean("person",Person.class);
3.1. 创建DefaultListableBeanFactory
首先创建DefaultListableBeanFactory对象DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
,并对其本身及其父类的属性进行初始化。这里我们主要关心的比如:
注册的Beandefinition被缓存在下面的Map中
//源码位置:org.springframework.beans.factory.support.DefaultListableBeanFactory#beanDefinitionMap
/** Map of bean definition objects, keyed by bean name. */
private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<>(256);
3.2. 创建XmlBeanDefinitionReader
创建BeanDefinition的读取器,它是读取xml并解析为BeanDefinition的调度控制中心,因此在创建该对象的时候,要把具有XxxRegistry基因的DefaultListableBeanFactory对象传入,XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
,以便于解析完成BeanDefinition之后,调用XxxRegistry的注册功能(该功能在DefaultListableBeanFactory#registerBeanDefinition中得到了具体的实现)缓存到Map中。
// 源码位置:org.springframework.beans.factory.xml.XmlBeanDefinitionReader#XmlBeanDefinitionReader
/**
* Create new XmlBeanDefinitionReader for the given bean factory.
* @param registry the BeanFactory to load bean definitions into,
* in the form of a BeanDefinitionRegistry
*/
public XmlBeanDefinitionReader(BeanDefinitionRegistry registry) {
super(registry);
}
从
XmlBeanDefinitionReader
的构造函数中的参数BeanDefinitionRegistry
我们也能看出,该Reader实际需要的只是一个XxxRegistry,而DefaultListableBeanFactory正是继承了BeanDefinitionRegistry
3.3. 加载BeanDefinition
根据xml资源文件加载BeanDefinition,因此这一步我们需要传入xml的Resource对象reader.loadBeanDefinitions(resource);
。
//源码位置:org.springframework.beans.factory.xml.XmlBeanDefinitionReader#loadBeanDefinitions(org.springframework.core.io.Resource)
/**
* Load bean definitions from the specified XML file.
* @param resource the resource descriptor for the XML file
* @return the number of bean definitions found
* @throws BeanDefinitionStoreException in case of loading or parsing errors
*/
@Override
public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException {
return loadBeanDefinitions(new EncodedResource(resource));
}
这里Spring使用EncodedResource
对Resource
进行了封装,用于对资源文件内容进行编码处理。我们看一下它是如何实现的:
// 源码位置:org.springframework.core.io.support.EncodedResource#getReader
/**
* Open a {@code java.io.Reader} for the specified resource, using the specified
* {@link #getCharset() Charset} or {@linkplain #getEncoding() encoding}
* (if any).
* @throws IOException if opening the Reader failed
* @see #requiresReader()
* @see #getInputStream()
*/
public Reader getReader() throws IOException {
if (this.charset != null) {
return new InputStreamReader(this.resource.getInputStream(), this.charset);
}
else if (this.encoding != null) {
return new InputStreamReader(this.resource.getInputStream(), this.encoding);
}
else {
return new InputStreamReader(this.resource.getInputStream());
}
}
getReader()
方法在资源文件创建输入流时,如果设置了编码属性,则会对其做相应的编码处理。
public int loadBeanDefinitions(Resource resource)
方法只做了一件事,就是使用EncodedResource对Resource资源进行封装,以支持文件编码的处理。
随后调用了重载方法public int loadBeanDefinitions(EncodedResource encodedResource)
,它接收EncodedResource类型的参数。在这里,该方法首先判断在当前线程中,该资源文件是否被重复加载。
private final ThreadLocal<Set<EncodedResource>> resourcesCurrentlyBeingLoaded =
new NamedThreadLocal<>("XML bean definition resources currently being loaded");
定义一个本地线程变量,以保证每个线程中的
Set<EncodedResource>
都是互不干扰。
// 源码位置:org.springframework.beans.factory.xml.XmlBeanDefinitionReader#loadBeanDefinitions(org.springframework.core.io.support.EncodedResource)
Set<EncodedResource> currentResources = this.resourcesCurrentlyBeingLoaded.get();
if (currentResources == null) {
currentResources = new HashSet<>(4);
this.resourcesCurrentlyBeingLoaded.set(currentResources);
}
if (!currentResources.add(encodedResource)) {
throw new BeanDefinitionStoreException(
"Detected cyclic loading of " + encodedResource + " - check your import definitions!");
}
判断EncodedResource文件资源是否被加载过或正在加载,当
Set<EncodedResource>
中已经存在该资源,再add的时候或返回false并抛出异常。
随后获取文件资源的输入流,正式进入BeanDefinition的加载流程doLoadBeanDefinitions(inputSource, encodedResource.getResource())
:
InputStream inputStream = encodedResource.getResource().getInputStream();
InputSource inputSource = new InputSource(inputStream);
return doLoadBeanDefinitions(inputSource, encodedResource.getResource());