Spring Beans 01 - Bean 配置读取

XML bean def conf 读取

BeanDefinitionReader

XmlBeanDefinitionReader 遵循 BeanDefinitionReader (其他非Java Config 的bean def reader 也遵循这个interface)。
BeanDefinitionReader 持有BeanDefinitionRegistry的ref,接受Resource object,读取解析后存入BeanDefinitionRegistry

//BeanDefinitionReader
BeanDefinitionRegistry getRegistry();
ResourceLoader getResourceLoader();
ClassLoader getBeanClassLoader();
BeanNameGenerator getBeanNameGenerator();
int loadBeanDefinitions(Resource... resources);
int loadBeanDefinitions(String... locations);

AbstractBeanDefinitionReader

接受外部传入registry。一般传入AppilcationContext。由于Application 亦实现了ResourceLoader接口,所以实际是把registry 和resource loader delete给了ApplicationContext。

    protected AbstractBeanDefinitionReader(BeanDefinitionRegistry registry) {
        Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
        this.registry = registry;

        // Determine ResourceLoader to use.
        if (this.registry instanceof ResourceLoader) {
            this.resourceLoader = (ResourceLoader) this.registry;
        }
        else {
            this.resourceLoader = new PathMatchingResourcePatternResolver();
        }

        // Inherit Environment if possible
        if (this.registry instanceof EnvironmentCapable) {
            this.environment = ((EnvironmentCapable) this.registry).getEnvironment();
        }
        else {
            this.environment = new StandardEnvironment();
        }
    }

AbstractBeanDefinitionReader 实现了从传入的location读取Resource 的逻辑,实际处理Resource 交由子类处理

public int loadBeanDefinitions(String location, @Nullable Set actualResources) {
  ...
            Resource[] resources = ((ResourcePatternResolver) resourceLoader).getResources(location);
            int loadCount = loadBeanDefinitions(resources);
    ...
}

XmlBeanDefinitionReader

loadBeanDefinitions(Resource resource) 顺序如下,先读取resource 成 xml document, 再逐个解析xml document 的元素。

  1. loadBeanDefinitions(EncodedResource encodedResource) 从Resource获取InputStream。调用doLoadBeanDefinitions(inputSource, encodedResource.getResource())
  2. Document doc = doLoadDocument(inputSource, resource) 使用DefaultDocumentLoader 加载XML 为 Document
Document doc = doLoadDocument(inputSource, resource);
return registerBeanDefinitions(doc, resource);
  1. registerBeanDefinitions(Document doc, Resource resource) 使用 DefaultBeanDefinitionDocumentReader 读取XML doc
BeanDefinitionDocumentReader documentReader = createBeanDefinitionDocumentReader();
documentReader.registerBeanDefinitions(doc, createReaderContext(resource));
  1. DocumentReader 读取非自定义的bean def。实际bean读取工作被delegate 给 BeanDefinitionParserDelegate
    private void parseDefaultElement(Element ele, BeanDefinitionParserDelegate delegate) {
        if (delegate.nodeNameEquals(ele, IMPORT_ELEMENT)) {
            importBeanDefinitionResource(ele);
        }
        else if (delegate.nodeNameEquals(ele, ALIAS_ELEMENT)) {
            processAliasRegistration(ele);
        }
        else if (delegate.nodeNameEquals(ele, BEAN_ELEMENT)) {
            processBeanDefinition(ele, delegate);
        }
        else if (delegate.nodeNameEquals(ele, NESTED_BEANS_ELEMENT)) {
            // recurse
            doRegisterBeanDefinitions(ele);
        }
    }
  1. BeanDefinitionParserDelegate 里面就是针对XML doc 元素的解析,下略。

你可能感兴趣的:(Spring Beans 01 - Bean 配置读取)