Spring provides support for extensible schemas, which typically require the following steps to complete a custom configuration:
- Design Configuration Properties and JavaBean
- Writing an XSD file
- Write Namespacehandler and Beandefinitionparser to complete the parsing work
- Write Spring.handlers and Spring.schemas all parts in tandem
- Apply in Bean file
All Dubbo tags in Dubbo are parsed using Dubbobeandefinitionparser, parsing XML tags into bean objects based on one-to-one attribute mappings.
Let's take Dubbo as an example of how to implement a label extension to a spring configuration.
1. Design Configuration Properties and JavaBean
The first thing we have to do is to design the configuration items and model them through JavaBean.
Taking Dubbo's Servicebean as an example, this defines Dubbo information for each service.
2. Writing an XSD file
The XSD file is located in the Dubbo-config-spring project, such as:
For example, the Dubbo:service to be used in the future:
<xsd:element name= "service" type= "ServiceType" >
<xsd:annotation>
<xsd:documentation><! [cdata[Export service config]]></xsd:documentation>
</xsd:annotation>
</xsd:element>
The servicetype type is defined as follows:
<xsd:complextype name= "ServiceType" >
<xsd:complexContent>
<xsd:extension base= "Abstractservicetype" >
<xsd:choice minoccurs= "0" maxoccurs= "unbounded" >
<xsd:element ref= "method" minoccurs= "0" maxoccurs= "unbounded"/>
<xsd:element ref= "parameter" minoccurs= "0" maxoccurs= "unbounded"/>
<xsd:element ref= "Beans:property" minoccurs= "0" maxoccurs= "unbounded"/>
</xsd:choice>
<xsd:attribute name= "interface" type= "Xsd:token" use= "required" >
<xsd:annotation>
<xsd:documentation><! [cdata[defines the interface to advertise for this service in the Service registry.] ></xsd:documentation>
<xsd:appinfo>
<tool:annotation>
<tool:expected-type type= "Java.lang.Class"/>
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name= "ref" Type= "xsd:string" use= "optional" >
<xsd:annotation>
<xsd:documentation><! [Cdata[The service implementation instance Bean ID.] ></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name= "class" Type= "xsd:string" use= "optional" >
<xsd:annotation>
<xsd:documentation><! [Cdata[the Service implementation class name.] ></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name= "path" type= "xsd:string" use= "optional" >
<xsd:annotation>
<xsd:documentation><! [cdata[the service path.] ></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name= "provider" type= "xsd:string" use= "optional" >
<xsd:annotation>
<xsd:documentation><! [cdata[Deprecated. Replace to protocol. ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name= "GENERIC" type= "xsd:string" use= "optional" >
<xsd:annotation>
<xsd:documentation><! [cdata[Generic Service.] ></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:anyattribute namespace= "# #other" processcontents= "lax"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
For example, the <xsd:attribute name= "ref" Type= "xsd:string" use= "optional" > the corresponding string-type configuration item ref.
3, write Namespacehandler and Beandefinitionparser complete parsing work
To complete the parsing work, the concepts of Namespacehandler and beandefinitionparser are used. Specifically, Namespacehandler will find a beandefinitionparser based on the schema and node name, and then beandefinitionparser to complete the specific parsing work. Therefore, the implementation classes of Namespacehandler and Beandefinitionparser need to be completed separately, Spring provides the default implementation classes Namespacehandlersupport and Abstractsinglebeandefinitionparser, and the simple way to do this is to inherit the two classes.
For example, Dubbo's Namespacehandler file is in Com.alibaba.dubbo.config.spring.schema.DubboNamespaceHandler.
Handler defines the objects that each configuration section resolves, such as code.
The Dubbobeandefinitionparser definition is also in this directory.
4. Write spring.handlers and Spring.schemas all parts in series
A few steps down the road will find that the development of handler and XSD can not make the application sense, so it is impossible to put the previous work into the system, Spring provides both the spring.handlers and Spring.schemas configuration files to do this, and these two files need to be written by ourselves and put into the Meta-inf folder, and the addresses of these two files must be meta-inf/ Spring.handlers and meta-inf/spring.schemas,spring will load them by default.
These two files, in the Dubbo project, are in the Dubbo-config-spring project, such as:
The contents of the Spring.handlers file are as follows:
Http\://code.alibabatech.com/schema/dubbo=com.alibaba.dubbo.config.spring.schema.dubbonamespacehandler
This indicates that when a schema reference named "Http://code.alibabatech.com/schema/dubbo" is used, will be resolved through Com.alibaba.dubbo.config.spring.schema.DubboNamespaceHandler.
The contents of the Spring.schemas file are as follows:
Http\://code.alibabatech.com/schema/dubbo/dubbo.xsd=meta-inf/dubbo.xsd
The above indicates the location of the XSD file loaded.
5. Apply in Bean file
Please refer to this article for Dubbo's spring configuration file:
Dubbo specific start-up services via Spring configuration (http://www.cnblogs.com/ghj1976/p/5320195.html)
In Dubbo-demo-provider, for example, its spring configuration file Dubbo-demo-provider.xml content is as follows:
<beans xmlns= "Http://www.springframework.org/schema/beans"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo= "Http://code.alibabatech.com/schema/dubbo"
xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-2.5.xsd
Http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd ">
<bean id= "Demoservice" class= "Com.alibaba.dubbo.demo.provider.DemoServiceImpl"/>
<dubbo:service interface= "Com.alibaba.dubbo.demo.DemoService" ref= "Demoservice"/>
</beans>
which
- Xmlns:dubbo= "Http://code.alibabatech.com/schema/dubbo" is used to specify a custom schema,
- The xsi:schemalocation is used to specify an XSD file.
- <dubbo:service interface= "Com.alibaba.dubbo.demo.DemoService" ref= "Demoservice"/> is a specific custom configuration usage instance.
Reference:
Provides custom configuration support based on spring extensible schema
http://blog.csdn.net/cutesource/article/details/5864562
Configuring tag extensions for spring in Dubbo