Spring源码解析(四)-DefaultListableBeanFactory和XmlBeanDefinitionReader

DefaultListableBeanFactory和XmlBeanFactoryfactory的关系

@Deprecated
@SuppressWarnings({"serial", "all"})
public class XmlBeanFactory extends DefaultListableBeanFactory {

    private final XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this);

    public XmlBeanFactory(Resource resource) throws BeansException {
        this(resource, null);
    }

    public XmlBeanFactory(Resource resource, BeanFactory parentBeanFactory) throws BeansException {
        super(parentBeanFactory);
        this.reader.loadBeanDefinitions(resource);
    }

}

从图中我们可以看出DefaultListableBeanFactory和是父子关系。从图中的@Deprecated可以看出XmlBeanFactoryfactory这个类已经被废弃了,那为什么我们这里还要介绍呢?细心的朋友可能已经发现这个类的构造函数就是我们第二章拆分出来的经典的四行代码

DefaultListableBeanFactory的构造函数和一个关键的成员变量

public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory
        implements ConfigurableListableBeanFactory, BeanDefinitionRegistry, Serializable {

/** key是bean的Name,value是BeanDefinition (核心)*/
private final Map beanDefinitionMap = new ConcurrentHashMap(256);

/**beanName的一个List,List中存的是上方Map的Key */
private volatile List beanDefinitionNames = new ArrayList(256);

/**单例模式beanName的一个Set集合,存的就是上方Map的Key */
private volatile Set manualSingletonNames = new LinkedHashSet(16);

public DefaultListableBeanFactory() {
    super();
}

public DefaultListableBeanFactory(BeanFactory parentBeanFactory) {
    super(parentBeanFactory);
}

XmlBeanDefinitionReader

XmlBeanDefinitionReader从字面意思上就可以理解Xml Bean定义解析器
下面我们来看一下XmlBeanDefinitionReader的构造函数和loadBeanDefinitions方法

XmlBeanDefinitionReader构造函数

/**
* Create new XmlBeanDefinitionReader for the given bean factory.
* @param registry the BeanFactory to load bean definitions into,
* in the form of a BeanDefinitionRegistry
* 这边为什么是BeanDefinitionRegistry?
* 从上图中可以发现DefaultListableBeanFactory 实现了BeanDefinitionRegistry接口
*/
public XmlBeanDefinitionReader(BeanDefinitionRegistry registry) {
    super(registry);
}

XmlBeanDefinitionReader的loadBeanDefinitions方法

    @Override
    public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException {
        //EncodedResource对象中有3个成员变量Resource resource;String encoding;Charset charset;encoding,charset默认为null
        return loadBeanDefinitions(new EncodedResource(resource));
    }

    /**
     * Load bean definitions from the specified XML file.
     * @param encodedResource the resource descriptor for the XML file,
     * allowing to specify an encoding to use for parsing the file
     * @return the number of bean definitions found
     * @throws BeanDefinitionStoreException in case of loading or parsing errors
     */
    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());
        }
       //resourcesCurrentlyBeingLoaded是一个ThreadLocal,获取线程局部变量,用法请参考ThreadLocal的博客
        Set 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!");
        }
        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();
            }
        }
    }

/**
     * Actually load bean definitions from the specified XML file.
     * @param inputSource the SAX InputSource to read from
     * @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
     * @see #doLoadDocument
     * @see #registerBeanDefinitions
     * 从XML文件中实际载入Bean定义资源的方法  
     */
    protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource)
            throws BeanDefinitionStoreException {
        try {
            //将XML文件转换为DOM对象,解析过程由documentLoader实现,
            //完成这个documentLoader是DefaultDocumentLoader,在定义documentLoader的地方创建
            Document doc = doLoadDocument(inputSource, resource);
            //注册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);
        }
    }

简单介绍下DefaultDocumentLoader

因为这里面都是dom解析中的api,故在此只做简单的介绍
XmlBeanDefinitionReader类中的doLoadDocument

private DocumentLoader documentLoader = new DefaultDocumentLoader();

protected Document doLoadDocument(InputSource inputSource, Resource resource) throws Exception {
        return this.documentLoader.loadDocument(inputSource, getEntityResolver(), this.errorHandler,
                getValidationModeForResource(resource), isNamespaceAware());
    }

DefaultDocumentLoader类中的loadDocument

/**
     * Load the {@link Document} at the supplied {@link InputSource} using the standard JAXP-configured
     * XML parser.
     */
    @Override
    public Document loadDocument(InputSource inputSource, EntityResolver entityResolver,
            ErrorHandler errorHandler, int validationMode, boolean namespaceAware) throws Exception {
        //创建文件解析器工厂
        DocumentBuilderFactory factory = createDocumentBuilderFactory(validationMode, namespaceAware);
        if (logger.isDebugEnabled()) {
            logger.debug("Using JAXP provider [" + factory.getClass().getName() + "]");
        }
        //创建文档解析器 
        DocumentBuilder builder = createDocumentBuilder(factory, entityResolver, errorHandler);
       //解析Spring的Bean定义资源 
        return builder.parse(inputSource);
    }

protected DocumentBuilderFactory createDocumentBuilderFactory(int validationMode, boolean namespaceAware)
            throws ParserConfigurationException {
        //创建文档解析工厂
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(namespaceAware);
        //XML校验
        if (validationMode != XmlValidationModeDetector.VALIDATION_NONE) {
            factory.setValidating(true);
            if (validationMode == XmlValidationModeDetector.VALIDATION_XSD) {
                // Enforce namespace aware for XSD...
                factory.setNamespaceAware(true);
                try {
                    factory.setAttribute(SCHEMA_LANGUAGE_ATTRIBUTE, XSD_SCHEMA_LANGUAGE);
                }
                catch (IllegalArgumentException ex) {
                    ParserConfigurationException pcex = new ParserConfigurationException(
                            "Unable to validate using XSD: Your JAXP provider [" + factory +
                            "] does not support XML Schema. Are you running on Java 1.4 with Apache Crimson? " +
                            "Upgrade to Apache Xerces (or Java 1.5) for full XSD support.");
                    pcex.initCause(ex);
                    throw pcex;
                }
            }
        }

        return factory;
    }

你可能感兴趣的:(Spring源码解析(四)-DefaultListableBeanFactory和XmlBeanDefinitionReader)