003-bean xml 解析流程

2020-03-15  本文已影响0人  当当一丢丢

一.需求

探讨Spring 配置文件 bean.xml 中元素是如何被解析的

二.xml构成划分

1.dispatcher-servlet.xml案例

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:util="http://www.springframework.org/schema/util"

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd  
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd  
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">

    <context:component-scan base-package="com.beijing"
    <bean id="student" class="com.beijing.Student">

</beans>

2.dispatcher-servlet.xml构成

2.1schema

schema即beans 标签属性, 大致分为两部分 xmlns, xsi:schemaLocation

    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd  

2.2namespace

2.3element

<xsd:schema xmlns="http://www.springframework.org/schema/context" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:tool="http://www.springframework.org/schema/tool" targetNamespace="http://www.springframework.org/schema/context" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xsd:import namespace="http://www.springframework.org/schema/beans" schemaLocation="https://www.springframework.org/schema/beans/spring-beans-4.1.xsd"/>
<xsd:import namespace="http://www.springframework.org/schema/tool" schemaLocation="https://www.springframework.org/schema/tool/spring-tool-4.1.xsd"/>
    <xsd:annotation>...</xsd:annotation>
    <xsd:complexType name="propertyPlaceholder">...</xsd:complexType>
    <xsd:element name="property-placeholder">...</xsd:element>
    <xsd:element name="property-override">...</xsd:element>
    <xsd:element name="annotation-config">...</xsd:element>
    <xsd:element name="component-scan">
        <xsd:annotation>...</xsd:annotation>
        <xsd:complexType>
            <xsd:sequence>...</xsd:sequence>
            <xsd:attribute name="base-package" type="xsd:string" use="required">...    </xsd:attribute>
            <xsd:attribute name="resource-pattern" type="xsd:string">...</xsd:attribute>
            <xsd:attribute name="use-default-filters" type="xsd:boolean" default="true">...</xsd:attribute>
            <xsd:attribute name="annotation-config" type="xsd:boolean" default="true">...</xsd:attribute>
            <xsd:attribute name="name-generator" type="xsd:string">...</xsd:attribute>
            <xsd:attribute name="scope-resolver" type="xsd:string">...</xsd:attribute>
            <xsd:attribute name="scoped-proxy">...</xsd:attribute>
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="load-time-weaver">...</xsd:element>
    <xsd:element name="spring-configured">...</xsd:element>
    <xsd:element name="mbean-export">...</xsd:element>
    <xsd:element name="mbean-server">...</xsd:element>
    <xsd:complexType name="filterType">...</xsd:complexType>
</xsd:schema>
image.png

三.标签的处理

我们知道,注解还是xml的标签,作用都是起一个tag的标记,最终是由其他处理器去发现这些标签,并根据标签执行不同的逻辑,所以上面这些namespace和element都对应着各自的处理器,且处理器以namespaceHandler:elementHandler层次结构封装

1.element处理器

http\://www.springframework.org/schema/context=org.springframework.context.config.ContextNamespaceHandler
http\://www.springframework.org/schema/jee=org.springframework.ejb.config.JeeNamespaceHandler
http\://www.springframework.org/schema/lang=org.springframework.scripting.config.LangNamespaceHandler
http\://www.springframework.org/schema/task=org.springframework.scheduling.config.TaskNamespaceHandler
http\://www.springframework.org/schema/cache=org.springframework.cache.config.CacheNamespaceHandler
public class ContextNamespaceHandler extends NamespaceHandlerSupport {

    @Override
    public void init() {
        registerBeanDefinitionParser("property-placeholder", new PropertyPlaceholderBeanDefinitionParser());
        registerBeanDefinitionParser("property-override", new PropertyOverrideBeanDefinitionParser());
        registerBeanDefinitionParser("annotation-config", new AnnotationConfigBeanDefinitionParser());
        registerBeanDefinitionParser("component-scan", new ComponentScanBeanDefinitionParser());
        registerBeanDefinitionParser("load-time-weaver", new LoadTimeWeaverBeanDefinitionParser());
        registerBeanDefinitionParser("spring-configured", new SpringConfiguredBeanDefinitionParser());
        registerBeanDefinitionParser("mbean-export", new MBeanExportBeanDefinitionParser());
        registerBeanDefinitionParser("mbean-server", new MBeanServerBeanDefinitionParser());
    }

}
public class AopNamespaceHandler extends NamespaceHandlerSupport {

    /**
     * Register the {@link BeanDefinitionParser BeanDefinitionParsers} for the
     * '{@code config}', '{@code spring-configured}', '{@code aspectj-autoproxy}'
     * and '{@code scoped-proxy}' tags.
     */
    @Override
    public void init() {
        // In 2.0 XSD as well as in 2.1 XSD.
        registerBeanDefinitionParser("config", new ConfigBeanDefinitionParser());
        registerBeanDefinitionParser("aspectj-autoproxy", new AspectJAutoProxyBeanDefinitionParser());
        registerBeanDefinitionDecorator("scoped-proxy", new ScopedProxyBeanDefinitionDecorator());

        // Only in 2.0 XSD: moved to context namespace as of 2.1
        registerBeanDefinitionParser("spring-configured", new SpringConfiguredBeanDefinitionParser());
    }

}  
上一篇 下一篇

猜你喜欢

热点阅读