天天看點

Spring配置檔案XML了解

前言

在Spring項目中我們經常看見applicationcontext.xml、spring-servlet.xml等XML檔案。那麼它的規則還有解析是怎麼樣的呢?
           

applicationcontext.XML

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    <description>Spring公共配置</description>

    <context:annotation-config/>

    <context:component-scan base-package="com.*.admin.module">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <context:exclude-filter type="annotation"
                                expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
    </context:component-scan>
    <context:property-placeholder properties-ref="customProperties" />
    <bean id="customProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean" p:ignoreResourceNotFound="true" p:localOverride="true">
        <property name="locations">
            <list>
                <value>classpath*:/application.properties</value>
            </list>
        </property>
    </bean>
</beans>
           

XML檔案有兩種解析方式DTD與XSD。由于現在Spring中大多使用XSD方式校驗,在此我主要分享XSD的校驗模式。(我檢視版本Spring 4.3.1.RELEASE)

XSD校驗簡介

XSD(XML Schemas Definition)。XSD描述了XML文檔的結構,用一個指定的XML Schema驗證XML文檔,并檢查該XML文檔是否符合其要求。使用XSD對XML檔案進行校驗需要

1、聲明名稱空間

xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           

2、指定該名稱空間對應的XSD檔案存儲位置

xsi:schemaLocation="
http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-4.0.xsd"
           

Spring中引用的XSD檔案

Spring配置檔案XML了解

applicationcontext.xml檔案

xmlns:context="http://www.springframework.org/schema/context"
//引入xsd檔案并在目前xml檔案起了别名叫context
<context:annotation-config/>
//context為别名,随xmlns:context定
//annotation-config是xsd需要校驗的節點(需要在xsd中存在才不會報錯)

    <context:component-scan base-package="com.*.admin.module">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <context:exclude-filter type="annotation"
                                expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
    </context:component-scan>
           

spring-context-4.0.xsd

annotation-config節點

//在校驗檔案存在該節點
    <xsd:element name="annotation-config">
        <xsd:annotation>
            //删除描述
        </xsd:annotation>
    </xsd:element>
           

component-scan節點描述

<xsd:element name="component-scan">
        <xsd:annotation>
            //删除描述
        </xsd:annotation>
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="include-filter" type="filterType"
                    minOccurs="0" maxOccurs="unbounded">
                    //在applicationContext.xml中使用include-filter
                    //type="filterType"引用下一代碼塊
                    <xsd:annotation>
                        <xsd:documentation><![CDATA[
    Controls which eligible types to include for component scanning.
                            ]]></xsd:documentation>
                    </xsd:annotation>
                </xsd:element>
                <xsd:element name="exclude-filter" type="filterType"
                    minOccurs="0" maxOccurs="unbounded">
                    <xsd:annotation>
                        <xsd:documentation><![CDATA[
    Controls which eligible types to exclude for component scanning.
                        ]]></xsd:documentation>
                    </xsd:annotation>
                </xsd:element>
            </xsd:sequence>
            <xsd:attribute name="base-package" type="xsd:string"
                use="required">
                <xsd:annotation>
                    <xsd:documentation><![CDATA[
    The comma/semicolon/space/tab/linefeed-separated list of packages to scan for annotated components.
                    ]]></xsd:documentation>
                </xsd:annotation>
            </xsd:attribute>
            <xsd:attribute name="resource-pattern" type="xsd:string">
                <xsd:annotation>
                    //删除描述
                </xsd:annotation>
            </xsd:attribute>
            <xsd:attribute name="use-default-filters" type="xsd:boolean"
                default="true">
                <xsd:annotation>
                    <xsd:documentation><![CDATA[
    Indicates whether automatic detection of classes annotated with @Component, @Repository, @Service,
    or @Controller should be enabled. Default is "true".
                    ]]></xsd:documentation>
                </xsd:annotation>
            </xsd:attribute>
            <xsd:attribute name="annotation-config" type="xsd:boolean"
                default="true">
                <xsd:annotation>
                    <xsd:documentation><![CDATA[
    Indicates whether the implicit annotation post-processors should be enabled. Default is "true".
                    ]]></xsd:documentation>
                </xsd:annotation>
            </xsd:attribute>
            <xsd:attribute name="name-generator" type="xsd:string">
                <xsd:annotation>
                    <xsd:documentation><![CDATA[
    The fully-qualified class name of the BeanNameGenerator to be used for naming detected components.
                    ]]></xsd:documentation>
                    <xsd:appinfo>
                        <tool:annotation>
                            <tool:expected-type type="java.lang.Class"/>
                            <tool:assignable-to type="org.springframework.beans.factory.support.BeanNameGenerator"/>
                        </tool:annotation>
                    </xsd:appinfo>
                </xsd:annotation>
            </xsd:attribute>
            <xsd:attribute name="scope-resolver" type="xsd:string">
                <xsd:annotation>
                    <xsd:documentation><![CDATA[
    The fully-qualified class name of the ScopeMetadataResolver to be used for resolving the scope of
    detected components.
                    ]]></xsd:documentation>
                    <xsd:appinfo>
                        <tool:annotation>
                            <tool:expected-type type="java.lang.Class"/>
                            <tool:assignable-to type="org.springframework.context.annotation.ScopeMetadataResolver"/>
                        </tool:annotation>
                    </xsd:appinfo>
                </xsd:annotation>
            </xsd:attribute>
            <xsd:attribute name="scoped-proxy">
                <xsd:annotation>
                    //删除描述
                </xsd:annotation>
                <xsd:simpleType>
                    <xsd:restriction base="xsd:string">
                        <xsd:enumeration value="no"/>
                        <xsd:enumeration value="interfaces"/>
                        <xsd:enumeration value="targetClass"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:attribute>
        </xsd:complexType>
    </xsd:element>
           

filterType定義

<xsd:complexType name="filterType">
        <xsd:attribute name="type" use="required">
            <xsd:annotation>
            //删除<xsd:documentation>描述
            </xsd:annotation>
            <xsd:simpleType>
                <xsd:restriction base="xsd:string">
                    <xsd:enumeration value="annotation"/>
                    <xsd:enumeration value="assignable"/>
                    <xsd:enumeration value="aspectj"/>
                    <xsd:enumeration value="regex"/>
                    <xsd:enumeration value="custom"/>
                </xsd:restriction>
            </xsd:simpleType>
        </xsd:attribute>
        <xsd:attribute name="expression" type="xsd:string" use="required">
            <xsd:annotation>
                <xsd:documentation><![CDATA[
    Indicates the filter expression, the type of which is indicated by "type".
                ]]></xsd:documentation>
            </xsd:annotation>
        </xsd:attribute>
    </xsd:complexType>
           

總括

在XML檔案進行校驗時

1、需要指定使用的校驗檔案(xmlns)并指定檔案位置(xsi:schemaLocation)

2、引入校驗檔案時指定别名(xmlns:context=”http://www.springframework.org/schema/context”)友善在xml檔案中使用

3、在使用XSD檔案中定義的标簽時需要驗證是否存在以及滿足要求

<context:annotation-config/>
<context:component-scan></context:component-scan>
           

4、在XML中使用标簽、屬性等都需要在引入的xsd檔案中存在并且其中的值也得符合規範(在xsd檔案中對值有type限制例如String、Class)不然會報錯