前言
在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文件
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)不然会报错