Spring IOC容器的初始化

时间:2022-01-04 19:40:39

前言

     1.在讲BeanDefinition的载入和解析之前,我们先来看看什么是BeanDefinition。

       Bean对象在Spring中是以BeanDefinition来描述的,也就是说在Spring中,BeanDefinition用来表示bean对象。

    2. 对于Spring IOC容器,BeanDefinition的载入过程,相当于把Spring的配置文件转换成Spring的内部数据结构。

在上一篇中容器初始化各个父类方法调用图,如下图所示:

Spring IOC容器的初始化

 

1.BeanDefinition的载入和解析

我们接着上一篇的代码,在AbstractBeanDefinitionReader的loadBeanDefinitions()中,我们定位到了Resource[],看下代码:

                              AbstractBeanDefinitionReader的loadBeanDefinitions

public int loadBeanDefinitions(String location, Set<Resource> actualResources) throws BeanDefinitionStoreException {
ResourceLoader resourceLoader
= getResourceLoader();
if (resourceLoader == null) {
throw new BeanDefinitionStoreException(
"Cannot import bean definitions from location [" + location + "]: no ResourceLoader available");
}

if (resourceLoader instanceof ResourcePatternResolver) {
// Resource pattern matching available.
try {
Resource[] resources
= ((ResourcePatternResolver) resourceLoader).getResources(location);
int loadCount = loadBeanDefinitions(resources);
if (actualResources != null) {
for (Resource resource : resources) {
actualResources.add(resource);
}
}
if (logger.isDebugEnabled()) {
logger.debug(
"Loaded " + loadCount + " bean definitions from location pattern [" + location + "]");
}
return loadCount;
}
catch (IOException ex) {
throw new BeanDefinitionStoreException(
"Could not resolve bean definition resource pattern [" + location + "]", ex);
}
}
else {
// Can only load single resources by absolute URL.
Resource resource = resourceLoader.getResource(location);
int loadCount = loadBeanDefinitions(resource);
if (actualResources != null) {
actualResources.add(resource);
}
if (logger.isDebugEnabled()) {
logger.debug(
"Loaded " + loadCount + " bean definitions from location [" + location + "]");
}
return loadCount;
}
}

1.1. 将xml文件转换成DOM对象

通过getResouce()方法得到Resouce之后,那么就开始了真正加载Bean资源。因为Spring可以对应不同形式的BeanDefition,我们这里讲的是xml的形式,所以就需要到xmlBeanDefinitonReader的实现中去看源码。

                                                    XmlBeanDefinitionReader的loadBeanDefinitions()及doLoadBeanDefinitions()

public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException {
return loadBeanDefinitions(new EncodedResource(resource));
}

 


public
int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException { Assert.notNull(encodedResource, "EncodedResource must not be null");
if (logger.isInfoEnabled()) {
logger.info(
"Loading XML bean definitions from " + encodedResource.getResource());
}

Set
<EncodedResource> currentResources = this.resourcesCurrentlyBeingLoaded.get();
if (currentResources == null) {
currentResources
= new HashSet<EncodedResource>(4);
this.resourcesCurrentlyBeingLoaded.set(currentResources);
}
if (!currentResources.add(encodedResource)) {
throw new BeanDefinitionStoreException(
"Detected cyclic loading of " + encodedResource + " - check your import definitions!");
}
try {
//将资源文件转换为IO输入流
InputStream inputStream
= encodedResource.getResource().getInputStream(); try {
InputSource inputSource
= new InputSource(inputStream);
if (encodedResource.getEncoding() != null) {
inputSource.setEncoding(encodedResource.getEncoding());
}
return doLoadBeanDefinitions(inputSource, encodedResource.getResource());
}
finally {
inputStream.close();
}
}
catch (IOException ex) {
throw new BeanDefinitionStoreException(
"IOException parsing XML document from " + encodedResource.getResource(), ex);
}
finally {
currentResources.remove(encodedResource);
if (currentResources.isEmpty()) {
this.resourcesCurrentlyBeingLoaded.remove();
}
}
}

 

//该方法-从特定XML文件中实际载入Bean定义资源
protected
int doLoadBeanDefinitions(InputSource inputSource, Resource resource) throws BeanDefinitionStoreException {
try {
//将XML文件转换为DOM对象
Document doc
= doLoadDocument(inputSource, resource); //这里是启动对Bean定义解析的详细过程,该解析过程会用到Spring的Bean配置规则[下文讲解]
return registerBeanDefinitions(doc, resource); }
catch (BeanDefinitionStoreException ex) {
throw ex;
}
catch (SAXParseException ex) {
throw new XmlBeanDefinitionStoreException(resource.getDescription(),
"Line " + ex.getLineNumber() + " in XML document from " + resource + " is invalid", ex);
}
catch (SAXException ex) {
throw new XmlBeanDefinitionStoreException(resource.getDescription(),
"XML document from " + resource + " is invalid", ex);
}
catch (ParserConfigurationException ex) {
throw new BeanDefinitionStoreException(resource.getDescription(),
"Parser configuration exception parsing XML from " + resource, ex);
}
catch (IOException ex) {
throw new BeanDefinitionStoreException(resource.getDescription(),
"IOException parsing XML document from " + resource, ex);
}
catch (Throwable ex) {
throw new BeanDefinitionStoreException(resource.getDescription(),
"Unexpected exception parsing XML document from " + resource, ex);
}
}

上面的过程完成了两个动作:(1)资源文件转换成IO输入流;(2)将xml文件转换成DOM对象。至于解析xml文件的方法,里面是通过创建解析工厂,设置xml解析校验,通过工厂创建解析器,然后parse(),所以这个过程我们就不看了

1.2 按照Spring的Bean规则对Document对象进行解析

走主线:得到dom对象后,开始启动对bean定义的详细解析,来看registerBeanDefinition()方法

                     XmlBeanDefinitionReader的loadBeanDefinitions()方法

public int registerBeanDefinitions(Document doc, Resource resource) throws BeanDefinitionStoreException {

BeanDefinitionDocumentReader documentReader
= createBeanDefinitionDocumentReader(); int countBefore = getRegistry().getBeanDefinitionCount();
//解析过程入口,BeanDefinitionDocumentReader只是个接口,具体的解析实现过程有实现类
//DefaultBeanDefinitionDocumentReader完成
documentReader.registerBeanDefinitions(doc, createReaderContext(resource));
return getRegistry().getBeanDefinitionCount() - countBefore;
}

 

我们总结下上面那么多绕人的代码。其实就2个思想:

(1)首先,通过调用XML解析器将Bean定义资源文件转换成DOM对象,但这些dom对象并没有按照Spring的bean规则进行解析。这一步是载入的过程。

(2)其次,在完成通用的XML解析之后,按照Spring的bean规则对document对象进行解析。

按照Spring的Bean规则对Document对象解析的过程是在接口BeanDefinitionDocumentReader的实现类DefaultBeanDefinitionDocumentReader中实现的。OK,重点来了。(友情提示,会绕好几个方法。)

                         DefaultBeanDefinitionDocumentReader的registerBeanDefinitions()及doRegisterBeanDefinitions()方法

    @Override
public void registerBeanDefinitions(Document doc, XmlReaderContext readerContext) {
this.readerContext = readerContext;
logger.debug(
"Loading bean definitions");
Element root
= doc.getDocumentElement();
doRegisterBeanDefinitions(root);
}
/**
* Register each bean definition within the given root {
@code <beans/>} element.
*/
protected void doRegisterBeanDefinitions(Element root) {
// Any nested <beans> elements will cause recursion in this method. In
// order to propagate and preserve <beans> default-* attributes correctly,
// keep track of the current (parent) delegate, which may be null. Create
// the new (child) delegate with a reference to the parent for fallback purposes,
// then ultimately reset this.delegate back to its original (parent) reference.
// this behavior emulates a stack of delegates without actually necessitating one.
BeanDefinitionParserDelegate parent = this.delegate;
this.delegate = createDelegate(getReaderContext(), root, parent);

if (this.delegate.isDefaultNamespace(root)) {
String profileSpec
= root.getAttribute(PROFILE_ATTRIBUTE);
if (StringUtils.hasText(profileSpec)) {
String[] specifiedProfiles
= StringUtils.tokenizeToStringArray(
profileSpec, BeanDefinitionParserDelegate.MULTI_VALUE_ATTRIBUTE_DELIMITERS);
if (!getReaderContext().getEnvironment().acceptsProfiles(specifiedProfiles)) {
return;
}
}
}

preProcessXml(root);
parseBeanDefinitions(root,
this.delegate);
postProcessXml(root);

this.delegate = parent;
}

 

/**
* 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);
}
}

上面这段代码又墨迹了一会,这里主要是看节点元素是否是Spring的规范,因为它允许我们自定义解析规范,那么正常我们是利用Spring的规则,所以我们来看parseDefaultElement(ele, delegate)方法

private void parseDefaultElement(Element ele, BeanDefinitionParserDelegate delegate) {
//如果元素节点是<import>导入元素,进行导入解析
if (delegate.nodeNameEquals(ele, IMPORT_ELEMENT)) { importBeanDefinitionResource(ele);
}
//如果元素节点时<Alias>别名元素,进行别名解析
else if (delegate.nodeNameEquals(ele, ALIAS_ELEMENT)) { processAliasRegistration(ele);
}
//如果是<Bean>元素,按照Spring的Bean规则解析元素
else if (delegate.nodeNameEquals(ele, BEAN_ELEMENT)) { processBeanDefinition(ele, delegate);
}
else if (delegate.nodeNameEquals(ele, NESTED_BEANS_ELEMENT)) {
// recurse
doRegisterBeanDefinitions(ele);
}
}

终于看到自己熟悉的一点东西了

1.3  Bean资源的解析

//解析bean资源
protected
void processBeanDefinition(Element ele, BeanDefinitionParserDelegate delegate) { //BeanDefinitionHolder是对BeanDefinition的封装,包括BeanDefinition,beanName,aliases
BeanDefinitionHolder bdHolder
= delegate.parseBeanDefinitionElement(ele); if (bdHolder != null) {
bdHolder
= delegate.decorateBeanDefinitionIfRequired(ele, bdHolder);
try {
//向SpringIOC容器注册解析得到的bean定义
//这是bean定义向IOC容器注册的入口,实际上是放到一个map里面

BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder, getReaderContext().getRegistry()); }
catch (BeanDefinitionStoreException ex) {
getReaderContext().error(
"Failed to register bean definition with name '" +
bdHolder.getBeanName()
+ "'", ele, ex);
}
// Send registration event.
getReaderContext().fireComponentRegistered(new BeanComponentDefinition(bdHolder));
}
}


注意:在解析<Bean>元素过程中没有创建和实例化Bean对象,只是创建了Bean对象的定义类型BeanDefinition,将<Bean>元素中的配置信息设置到BeanDefiniton作记录,当依赖注入时才使用这些记录信息创建和实例化具体的Bean对象。