spring-schema文件解析(-)

由于作者这方面也是新手,故而大神可以直接略过,权当自己留下的笔记,谬误之处勿喷~
要学习spring schemas及其解析过程,我们应该首先:
1. 熟悉XML/XHTML的基本指示
2. w3school schema课程

3. java及Spring熟练使用

接下来正式开始

首先来看一个spring-beans-4.3.xsd的文件主要内容:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<xsd:schema xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://www.springframework.org/schema/beans">

    <xsd:import namespace="http://www.w3.org/XML/1998/namespace"/>

    <xsd:annotation>
        <xsd:documentation><![CDATA[
	Spring XML Beans Schema, version 4.3
	Authors: Juergen Hoeller, Rob Harrop, Mark Fisher, Chris Beams

	This defines a simple and consistent way of creating a namespace
	of JavaBeans objects, managed by a Spring BeanFactory, read by
	XmlBeanDefinitionReader (with DefaultBeanDefinitionDocumentReader).

	This document type is used by most Spring functionality, including
	web application contexts, which are based on bean factories.

	Each "bean" element in this document defines a JavaBean.
	Typically the bean class is specified, along with JavaBean properties
	and/or constructor arguments.

	A bean instance can be a "singleton" (shared instance) or a "prototype"
	(independent instance). Further scopes can be provided by extended
	bean factories, for example in a web environment.

	References among beans are supported, that is, setting a JavaBean property
	or a constructor argument to refer to another bean in the same factory
	(or an ancestor factory).

	As alternative to bean references, "inner bean definitions" can be used.
	Such inner beans do not have an independent lifecycle; they are typically
	anonymous nested objects that share the scope of their containing bean.

	There is also support for lists, sets, maps, and java.util.Properties
	as bean property types or constructor argument types.
		]]></xsd:documentation>
    </xsd:annotation>

    <!-- base types -->
    <!--抽象的基础符合类型-->
    <xsd:complexType name="identifiedType" abstract="true">
        <xsd:annotation>
            <xsd:documentation><![CDATA[
	The unique identifier for a bean. The scope of the identifier
	is the enclosing bean factory.
			]]></xsd:documentation>
        </xsd:annotation>
        <xsd:attribute name="id" type="xsd:string">
            <xsd:annotation>
                <xsd:documentation><![CDATA[
	The unique identifier for a bean. A bean id may not be used more than once
	within the same <beans> element.
				]]></xsd:documentation>
            </xsd:annotation>
        </xsd:attribute>
    </xsd:complexType>

    <!-- Top-level <beans> tag -->
    <!-- beans标签 -->
    <!--doc: beans标签bean元素和其他元素的容器,也是xml配置文件的根元素,允许所有嵌套bean的默认值。 -->
    <!--beans标签也可以嵌套于beans标签体内 ---- 出于定义一个有确定默认值的beans子集或者用于注册profles并激活profile的目的-->
    <!--注意:此类beans标签必须被声明为spring beans xml config的最后一个元素-->
    <xsd:element name="beans">
        <xsd:annotation>
            <xsd:documentation><![CDATA[
	Container for <bean> and other elements, typically the root element in the document.
	Allows the definition of default values for all nested bean definitions. May itself
	be nested for the purpose of defining a subset of beans with certain default values or
	to be registered only when certain profile(s) are active. Any such nested <beans> element
	must be declared as the last element in the document.
			]]></xsd:documentation>
        </xsd:annotation>
        <xsd:complexType>
            <xsd:sequence>
                <!--定义了 description, import, alias, bean, beans标签,每个标签都一个出现一次或多次。
                    其中description必须放在第一位,可以出现0此或多次;
                    其他三个标签可以没有先后顺序,也可以是出现0次或多次
                    beans标签必须放在末位;

                -->
                <!--
                    定义了一个可选的属性:profile,类型string
                -->
                <xsd:element ref="description" minOccurs="0"/>
                <xsd:choice minOccurs="0" maxOccurs="unbounded">
                    <xsd:element ref="import"/>
                    <xsd:element ref="alias"/>
                    <xsd:element ref="bean"/>
                    <xsd:any namespace="##other" processContents="strict" minOccurs="0" maxOccurs="unbounded"/>
                </xsd:choice>
                <xsd:element ref="beans" minOccurs="0" maxOccurs="unbounded"/>
            </xsd:sequence>
            <xsd:attribute name="profile" use="optional" type="xsd:string">
                <!--profiles的集合是为了确定将要解析的beans集合,即是此刻哪一个beans标签体内的元素将要被解析,
                多个profile可以用空格, 逗号, 分号分割-->
                <!--如果在激活文件时,一个或多个profile被激活,则对应的beans标签体内的元素被解析, 其中所有的bean都会被注册 -->
                <!--如果解析时,没有对应的profile被激活,那么全部的元素和它的内容都将被忽略。-->
                <!--如果一个profile前缀加上了否定符号"!"
                    <beans profile="p1,!p2">
                    表明p1被激活而p2未被激活
                -->
                <!--profiles可通过以下两种方式被激活:
                程序式配置:
                    ConfigurableEnvironment#setActiveProfiles(String...)
			        ConfigurableEnvironment#setDefaultProfiles(String...)
			    属性配置式(可通过启动时的 -D 系统属性, 环境变量或者servlet context init params):
			        spring.profiles.active=p1,p2
			        spring.profiles.default=p1,p2
                -->
                <xsd:annotation>
                    <xsd:documentation><![CDATA[
	The set of profiles for which this <beans> element should be parsed. Multiple profiles
	can be separated by spaces, commas, or semi-colons.

	If one or more of the specified profiles are active at time of parsing, the <beans>
	element will be parsed, and all of its <bean> elements registered, <import>
	elements followed, etc.  If none of the specified profiles are active at time of
	parsing, then the entire element and its contents will be ignored.

	If a profile is prefixed with the NOT operator '!', e.g.

		<beans profile="p1,!p2">

	indicates that the <beans> element should be parsed if profile "p1" is active or
	if profile "p2" is not active.

	Profiles are activated in one of two ways:
		Programmatic:
			ConfigurableEnvironment#setActiveProfiles(String...)
			ConfigurableEnvironment#setDefaultProfiles(String...)

		Properties (typically through -D system properties, environment variables, or
		servlet context init params):
			spring.profiles.active=p1,p2
			spring.profiles.default=p1,p2
					]]></xsd:documentation>
                </xsd:annotation>
            </xsd:attribute>
            <!--定义默认属性:默认延迟加载:default-lazy-init:-->
            <!--doc: 默认的延迟加载值,请看bean标签的lazy-init属性, 默认值是default,表示从嵌套的外部beans中继承,否则返回false -->
            <xsd:attribute name="default-lazy-init" default="default" type="defaultable-boolean">
                <xsd:annotation>
                    <xsd:documentation><![CDATA[
	The default 'lazy-init' value; see the documentation for the
	'lazy-init' attribute of the 'bean' element. The default is "default",
	indicating inheritance from outer 'beans' sections in case of nesting,
	otherwise falling back to "false".
					]]></xsd:documentation>
                </xsd:annotation>
            </xsd:attribute>
            <!--默认的“合并”值; 请参阅各种集合元素的“合并”属性文档。 缺省值为“default”,表示嵌套时从外部“bean”部分继承,否则返回“false”。-->
            <xsd:attribute name="default-merge" default="default" type="defaultable-boolean">
                <xsd:annotation>
                    <xsd:documentation><![CDATA[
	The default 'merge' value; see the documentation for the 'merge'
	attribute of the various collection elements. The default is "default",
	indicating inheritance from outer 'beans' sections in case of nesting,
	otherwise falling back to "false".
					]]></xsd:documentation>
                </xsd:annotation>
            </xsd:attribute>
            <!--默认的autowire值, 请参阅bean元素的autowire属性文档, 默认值是default, 表明嵌套时从外部beans继承, 否则返回false-->
            <!--无外部驱动的自动装配-->
            <!--定义了autowire的自动装配的可选枚举值: default, no, byname, byType, constructor-->
            <xsd:attribute name="default-autowire" default="default">
                <xsd:annotation>
                    <xsd:documentation><![CDATA[
	The default 'autowire' value; see the documentation for the
	'autowire' attribute of the 'bean' element. The default is "default",
	indicating inheritance from outer 'beans' sections in case of nesting,
	otherwise falling back to "no" (i.e. no externally driven autowiring).
					]]></xsd:documentation>
                </xsd:annotation>
                <xsd:simpleType>
                    <xsd:restriction base="xsd:NMTOKEN">
                        <xsd:enumeration value="default"/>
                        <xsd:enumeration value="no"/>
                        <xsd:enumeration value="byName"/>
                        <xsd:enumeration value="byType"/>
                        <xsd:enumeration value="constructor"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:attribute>
            <!--这个部分没有太看懂,看个大概 -->
            <!--默认装配候选值:
                用于标识autowire的候选值的默认bean名称匹配模式, 如*Service, *Dao, 或者 “*Service, *Dao”
                详细请参阅bean的autowire-candidate属性的更多细节
            -->
            <xsd:attribute name="default-autowire-candidates" type="xsd:string">
                <xsd:annotation>
                    <xsd:documentation><![CDATA[
	A default bean name pattern for identifying autowire candidates:
	e.g. "*Service", "data*", "*Service*", "data*Service".
	Also accepts a comma-separated list of patterns: e.g. "*Service,*Dao".
	See the documentation for the 'autowire-candidate' attribute of the
	'bean' element for the semantic details of autowire candidate beans.
					]]></xsd:documentation>
                </xsd:annotation>
            </xsd:attribute>
            <!--默认初始化方法: 参看bena的init-method属性-->
            <xsd:attribute name="default-init-method" type="xsd:string">
                <xsd:annotation>
                    <xsd:documentation><![CDATA[
	The default 'init-method' value; see the documentation for the
	'init-method' attribute of the 'bean' element.
					]]></xsd:documentation>
                </xsd:annotation>
            </xsd:attribute>
            <!--默认销毁方法: 请看bean元素的destroy-method属性-->
            <xsd:attribute name="default-destroy-method" type="xsd:string">
                <xsd:annotation>
                    <xsd:documentation><![CDATA[
	The default 'destroy-method' value; see the documentation for the
	'destroy-method' attribute of the 'bean' element.
					]]></xsd:documentation>
                </xsd:annotation>
            </xsd:attribute>
            <xsd:anyAttribute namespace="##other" processContents="lax"/>
        </xsd:complexType>
    </xsd:element>

    <!--beans元素默认引用的第一个元素,必须出现在beans元素的首个元素,可出现0次或多次 -->
    <!--包含描述封闭元素用途的信息文本。主要用于XML bean定义文档的用户参考手册。-->
    <xsd:element name="description">
        <xsd:annotation>
            <xsd:documentation><![CDATA[
	Contains informative text describing the purpose of the enclosing element.
	Used primarily for user documentation of XML bean definition documents.
			]]></xsd:documentation>
        </xsd:annotation>
        <xsd:complexType mixed="true">
            <xsd:choice minOccurs="0" maxOccurs="unbounded"/>
        </xsd:complexType>
    </xsd:element>

    <!--import元素:声明要导入的xml bean定义源文件-->
    <!--属性:
        resource:必须指定。用于指定xml源文件的相对路径
    -->
    <xsd:element name="import">
        <xsd:annotation>
            <xsd:documentation source="java:org.springframework.core.io.Resource"><![CDATA[
	Specifies an XML bean definition resource to import.
			]]></xsd:documentation>
        </xsd:annotation>
        <xsd:complexType>
            <xsd:complexContent>
                <xsd:restriction base="xsd:anyType">
                    <xsd:attribute name="resource" type="xsd:string" use="required">
                        <xsd:annotation>
                            <xsd:documentation><![CDATA[
	The relative resource location of the XML (bean definition) file to import,
	for example "myImport.xml" or "includes/myImport.xml" or "../myImport.xml".
							]]></xsd:documentation>
                        </xsd:annotation>
                    </xsd:attribute>
                </xsd:restriction>
            </xsd:complexContent>
        </xsd:complexType>
    </xsd:element>

    <!--alias元素:为一个bean定义别名(该bean可以声明在不同的源文件中)-->
    <!--属性:
        name: 需要被定义别名的bean, 必须
        alias: bean的别名,必须
    -->
    <xsd:element name="alias">
        <xsd:annotation>
            <xsd:documentation><![CDATA[
	Defines an alias for a bean (which can reside in a different definition
	resource).
			]]></xsd:documentation>
        </xsd:annotation>
        <xsd:complexType>
            <xsd:complexContent>
                <xsd:restriction base="xsd:anyType">
                    <xsd:attribute name="name" type="xsd:string" use="required">
                        <xsd:annotation>
                           
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值