<!-- Template渲染服务。 --> <services:template xmlns="http://www.alibaba.com/schema/services/template/engines" searchExtensions="true"> <velocity-engine templateEncoding="GBK" strictReference="false" path="/templates/${component}"> <global-macros> <name>global/*.vm</name> </global-macros> <plugins> <vm-plugins:escape-support defaultEscape="html"> <vm-plugins:noescape> <vm-plugins:if-matches pattern="^control\." /> <vm-plugins:if-matches pattern="^screen_placeholder" /> <vm-plugins:if-matches pattern="^stringEscapeUtil\.escape" /> <vm-plugins:if-matches pattern="^csrfToken\.(get)?hiddenField" /> </vm-plugins:noescape> </vm-plugins:escape-support> </plugins> </velocity-engine> </services:template>
services是root扩展点,template是对services的捐献,同时template的element也是扩展点,namespace:http://www.alibaba.com/schema/services/template/engines,详见template.xsd:
<xsd:complexType name="TemplateServiceType"> <xsd:choice minOccurs="0" maxOccurs="unbounded"> <xsd:element name="template-mapping" type="TemplateServiceEngineMappingType" minOccurs="0" maxOccurs="unbounded"> <xsd:annotation> <xsd:documentation><![CDATA[ 将模板引擎和模板名后缀关联起来。 ]]></xsd:documentation> </xsd:annotation> </xsd:element> <xsd:any namespace="http://www.alibaba.com/schema/services/template/engines" /> </xsd:choice> <xsd:attribute name="defaultExtension" type="xsd:string" /> <xsd:attribute name="searchExtensions" type="springext:booleanOrPlaceholder" /> <xsd:attribute name="searchLocalizedTemplates" type="springext:booleanOrPlaceholder" /> <xsd:attribute name="cacheEnabled" type="springext:booleanOrPlaceholder" /> <xsd:attributeGroup ref="springext:identifiedTypeAttributeSubset" /> </xsd:complexType> <xsd:complexType name="TemplateServiceEngineMappingType"> <xsd:attribute name="extension" type="xsd:string" use="required" /> <xsd:attribute name="engine" type="xsd:string" use="required" /> </xsd:complexType>
template的属性:searchExtensions等。
template的elements:global-macros, plugins。
global-macros是一个list sting
plugins又是一个扩展点.vm-plugins是其中的一种捐献。
对应的xsd描述:
<xsd:complexType name="VelocityTemplateEngineType"> <xsd:complexContent> <xsd:extension base="beans:identifiedType"> <xsd:all> <xsd:element name="global-macros" type="VelocityGlobalMacrosType" minOccurs="0"> <xsd:annotation> <xsd:documentation><![CDATA[ 装载全局宏(相对于path路径),可使用通配符。 ]]></xsd:documentation> </xsd:annotation> </xsd:element> <xsd:element name="plugins" type="VelocityPluginsType" minOccurs="0"> <xsd:annotation> <xsd:documentation><![CDATA[ Velocity插件和事件处理器。 ]]></xsd:documentation> </xsd:annotation> </xsd:element> <xsd:element name="advanced-properties" type="VelocityPropertiesType" minOccurs="0"> <xsd:annotation> <xsd:documentation><![CDATA[ Velocity高级设置,参见velocity文档: (http://velocity.apache.org/engine/releases/velocity-1.6.2/developer-guide.html#Velocity_Configuration_Keys_and_Values) 和默认配置文件:org/apache/velocity/runtime/defaults/velocity.properties。 ]]></xsd:documentation> </xsd:annotation> </xsd:element> </xsd:all> <xsd:attribute name="path" type="xsd:string" default="/templates" /> <xsd:attribute name="templateEncoding" type="xsd:string" default="UTF-8" /> <xsd:attribute name="cacheEnabled" type="springext:booleanOrPlaceholder" default="true" /> <xsd:attribute name="modificationCheckInterval" type="springext:integerOrPlaceholder" default="2" /> <xsd:attribute name="strictReference" type="springext:booleanOrPlaceholder" default="true" /> </xsd:extension> </xsd:complexContent> </xsd:complexType>
其中的
<xsd:element name="global-macros" type="VelocityGlobalMacrosType" minOccurs="0"> <xsd:element name="plugins" type="VelocityPluginsType" minOccurs="0">
定义了element的名字和类型的映射关系,下面是type的描述:
<xsd:complexType name="VelocityGlobalMacrosType"> <xsd:sequence> <xsd:element name="name" type="xsd:string" minOccurs="0" maxOccurs="unbounded" /> </xsd:sequence> </xsd:complexType> <xsd:complexType name="VelocityPluginsType"> <xsd:sequence> <xsd:any namespace="http://www.alibaba.com/schema/services/template/engines/velocity/plugins" minOccurs="0" maxOccurs="unbounded" /> </xsd:sequence> </xsd:complexType>
xsd:sequence表示list,xsd:any表示所有满足namespace="http://www.alibaba.com/schema/services/template/engines/velocity/plugins"都行。其实就是定义了一处扩展点。
二.VelocityEngineDefinitionParser类
再来看看VelocityEngineDefinitionParser如何来parser。
VelocityEngineDefinitionParser 继承了 AbstractSingleBeanDefinitionParser,但是要注意如果是顶级service要继承AbstractNamedBeanDefinitionParser,实现其getDefaultName方法。否则会抛出异常。