spring5ソースコード解析シリーズ(4) - 初期IOC容器(B)

はじめに:XMLビーンリーダー(XmlBeanDefinitionReader)メソッド呼び出しreader.loadBeanDefinitionsの話前の親AbstractBeanDefinitionReaderビーンがリソースの定義を読んで、私たちはこの記事に動いています。

loadBeanDefinitions方法AbstractBeanDefinitionReader(5)

次のようにソース方法:

//重载方法,调用下面的loadBeanDefinitions(String, Set<Resource>)方法
@Override
public int loadBeanDefinitions(String location) throws BeanDefinitionStoreException {
    return loadBeanDefinitions(location, null);
}

public int loadBeanDefinitions(String location, @Nullable Set<Resource> actualResources) throws BeanDefinitionStoreException {
    //获取在IoC容器初始化过程中设置的资源加载器
    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 {
            //将指定位置的Bean定义资源文件解析为Spring IOC容器封装的资源
            //加载多个指定位置的Bean定义资源文件
            Resource[] resources = ((ResourcePatternResolver) resourceLoader).getResources(location);
            //委派调用其子类XmlBeanDefinitionReader的方法,实现加载功能
            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.
        //将指定位置的Bean定义资源文件解析为Spring IOC容器封装的资源
        //加载单个指定位置的Bean定义资源文件
        Resource resource = resourceLoader.getResource(location);
        //委派调用其子类XmlBeanDefinitionReader的方法,实现加载功能
        int loadCount = loadBeanDefinitions(resource);
        if (actualResources != null) {
            actualResources.add(resource);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Loaded " + loadCount + " bean definitions from location [" + location + "]");
        }
        return loadCount;
    }
}

//重载方法,调用loadBeanDefinitions(String);
@Override
public int loadBeanDefinitions(String... locations) throws BeanDefinitionStoreException {
    Assert.notNull(locations, "Location array must not be null");
    int counter = 0;
    for (String location : locations) {
        counter += loadBeanDefinitions(location);
    }
    return counter;
}

loadBeanDefinitions(リソース...リソース)もloadBeanDefinitionsメソッドXmlBeanDefinitionReaderを呼び出すの一種です。

AbstractBeanDefinitionReaderソースコード分析のLoadBeanDefinitions方法は、この方法を見ることができる:
まず、リソースresourceLoader.getResource(場所)へのアクセスのリソースローダメソッドを呼び出すロードされるリソースを取得するために、
第二に、負荷の関数の実際の実行はloadBeanDefinitions XmlBeanDefinitionReaderのサブクラスであります方法。

(6)資源・ローダは、リソースを読み込むために取得します

リソースへのアクセスは、親クラスDefaultResourceLoaderのgetResourceメソッドを呼び出すことによってロードされるAbstractBeanDefinitionReader次のように、ソースコードは次のとおりです。

//获取Resource的具体实现方法
@Override
public Resource getResource(String location) {
    Assert.notNull(location, "Location must not be null");

    for (ProtocolResolver protocolResolver : this.protocolResolvers) {
        Resource resource = protocolResolver.resolve(location, this);
        if (resource != null) {
            return resource;
        }
    }
    //如果是类路径的方式,需要使用ClassPathResource来得到bean文件的资源对象
    if (location.startsWith("/")) {
        return getResourceByPath(location);
    }
    else if (location.startsWith(CLASSPATH_URL_PREFIX)) {
        return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader());
    }
    else {
        try {
            // Try to parse the location as a URL...
            // 如果是URL方式,使用UrlResource作为bean文件的资源对象
            URL url = new URL(location);
            return (ResourceUtils.isFileURL(url) ? new FileUrlResource(url) : new UrlResource(url));
        }
        catch (MalformedURLException ex) {
            // No URL -> resolve as resource path.
            //如果既不是classpath标识,又不是URL标识的Resource定位,则调用容器本身的getResourceByPath方法获取Resource
            return getResourceByPath(location);
        }
    }
}

FileSystemXmlApplicationContext getResourceByPath容器メソッドの実装を提供し、処理はいずれもクラスパスを識別することで、URLリソース位置決め場合によって同定されていません。

@Override
protected Resource getResourceByPath(String path) {
    if (path.startsWith("/")) {
        path = path.substring(1);
    }
    //这里使用文件系统资源对象来定义bean文件
    return new FileSystemResource(path);
}

このようなコードは、彼がファイルシステムから設定ファイルを取得するための完全なリソース定義FileSystemResourceを提供し、過去にFileSystemXmlApplicationContextに返されます。
これはIOCにロードされ、当然のことながら、このロジックに応じて任意の場所からロードすることができ、あなたは、リソースのすべての種類を見ることができ、それは春、で抽象化を提供し、構成ファイルへのファイルシステムパスから行うことができ、
私たちのために同じようなClassPathResource、URLResource、FileSystemResourceなど使用しています。上記プロセスは、ロードプロセスのほんの一部であるリソースを配置することです。

(7)XmlBeanDefinitionReaderは、リソースビーンの定義をロード

loadBeanDefinitionsのバックXmlBeanDefinitionReader(リソース...)メソッドは、Beanのリソース定義ファイルのロード処理に代わって未来を見ることができます。

//XmlBeanDefinitionReader加载资源的入口方法
@Override
public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException {
    //将读入的XML资源进行特殊编码处理
    return loadBeanDefinitions(new EncodedResource(resource));
}

//这里是载入XML形式Bean定义资源文件方法
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<>(4);
        this.resourcesCurrentlyBeingLoaded.set(currentResources);
    }
    if (!currentResources.add(encodedResource)) {
        throw new BeanDefinitionStoreException(
                "Detected cyclic loading of " + encodedResource + " - check your import definitions!");
    }
    try {
        //将资源文件转为InputStream的IO流
        InputStream inputStream = encodedResource.getResource().getInputStream();
        try {
            //从InputStream中得到XML的解析源
            InputSource inputSource = new InputSource(inputStream);
            if (encodedResource.getEncoding() != null) {
                inputSource.setEncoding(encodedResource.getEncoding());
            }
            //这里是具体的读取过程
            return doLoadBeanDefinitions(inputSource, encodedResource.getResource());
        }
        finally {
            //关闭从Resource中得到的IO流
            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对象,解析过程由documentLoader实现
        Document doc = doLoadDocument(inputSource, resource);
        //这里是启动对Bean定义解析的详细过程,该解析过程会用到Spring的Bean配置规则
        return registerBeanDefinitions(doc, resource);
    }
    ...
}

ソースコードを解析することによって、Beanの最後のステップは、豆は、ドキュメントオブジェクトに変換リソースを定義するリソース・ファイルを定義してロードされ処理がdocumentLoaderによって実現されます。

(8)DocumentLoader Beanが定義されたリソースドキュメントオブジェクトに変換します

ソースとして、次のとおりです。

//使用标准的JAXP将载入的Bean定义资源转换成document对象
@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;
}

処理のための分析プロシージャコールJavaEEの標準JAXP標準。この時点でビーン春IOCコンテナリソースファイルの場所の定義によれば、負荷は読み出しおよびドキュメントオブジェクト工程に変換完了する。
その後、我々はそれが春IOC Beanオブジェクトの管理に解決し、コンテナに登録されている方法Documentオブジェクトへビーン定義されたリソースファイルをロードする春IOCコンテナの後に分析し続けます。

IOCコンテナの初期化より多くのコンテンツ、これは2枚目の歓迎の注目は、残りの部分に感謝の書き込みに数分です!

おすすめ

転載: www.cnblogs.com/yaofengdoit/p/12094055.html