The principle of spring custom labels
XML is typically defined by DTDs, XSD, but the DTD is less expressive, the XSD definition is more powerful, it can define types, occurrences, and so on. Custom labels require XSD support and, when implemented, use the namespace extension to support custom labels.
<bean id= "Beanid" class= "com.xxx.xxxx.Xxxxx" > <property name= "Property1" > <value>xxxx </value> </property> <property name= "Property2" > <value>XXXX</value> </property>
When you write the above code in a bitter force:
would you be envious of writing code like this?
<xxx:xxxx id= "Beanid"/>
XML code
Spring parses it into a DOM tree through an XML parser and converts it to beandefinition by Namespacehandler specifying the corresponding namespace Beandefinitionparser. The object is instantiated by the function of spring itself on the beandefinition.
During this time, spring will also load two materials:
- meta-inf/spring.handlers
Specify Namespacehandler (implement org.springframework.beans.factory.xml.namespacehandler ) interface, or use
- Meta-inf/spring.schemas
Redirects the XSD to a local file when parsing the XML file, avoiding the need to download the XSD file online when parsing the XML file. This function is realized through the real org.xml.sax.EntityResolver interface.
Make a custom Label
spring.handlers:
XML code
- http\://test.hatter.me/schema/ Test = Me . Hatter.test.TestNamespaceHandler
Spring.schemas :
XML code
- http\://test.hatter.me/schema/test/ test.xsd = META -inf/test.xsd
test.xsd :
XML code
<?xml version= "1.0" encoding= "UTF-8" standalone= "no"?> <xsd:schema xmlns= "http://test.hatter.me/ Schema/test " xmlns:xsd=" Http://www.w3.org/2001/XMLSchema " targetnamespace=" http://test.hatter.me/ Schema/test "> <xsd:element name=" Custom "type=" Customtype "> </xsd:element> <xsd: ComplexType name= "Customtype" > <xsd:attribute name= "id" type= "xsd:id" > </xsd:attribute> <xsd:attribute name= "name" type= "xsd:string" > </xsd:attribute> </xsd:complextype > </xsd:schema>
Me.hatter.test.TestNamespaceHandler:
Java code
Package me.hatter.test; Import Org.springframework.beans.factory.xml.NamespaceHandlerSupport; public class Testnamespacehandler extends Namespacehandlersupport {public void init () { Registerbeandefinitionparser ("Custom", New Testcustombeandefinitionparser ()); } }
Me.hatter.test.TestCustomBeanDefinitionParser:
Java code
" /span>
Package me.hatter.test; Import Me.hatter.test.bean.TestBean; Import org.springframework.beans.factory.config.BeanDefinition; Import org.springframework.beans.factory.support.RootBeanDefinition; Import Org.springframework.beans.factory.xml.BeanDefinitionParser; Import Org.springframework.beans.factory.xml.ParserContext; Import org.w3c.dom.Element; public class Testcustombeandefinitionparser implements Beandefinitionparser {public beandefinition parse (E lement element, ParserContext parsercontext) {String id = element.getattribute ("id"); String name = Element.getattribute ("name"); Rootbeandefinition beandefinition = new Rootbeandefinition (); Beandefinition.setbeanclass (Testbean.class); Beandefinition.getpropertyvalues (). Addpropertyvalue ("name", name); Parsercontext.getregistry (). Registerbeandefinition (ID, beandefinition); return beandefinition; } }
Test code
Test.xml:
XML code
-
<?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:test= "Http://test.hatter.me/schema/test" xsi:schemalocation= " Http://www.springframework.org/schema/beans Http://www.springframework.org/schema /beans/spring-beans-2.5.xsd http://test.hatter.me/schema/test http://test.hatter.me/schema/test/test.xsd " > <test:custom id= "Testcustom" Name= "This is a test custom tag"/> </beans>
Me.hatter.test.main.Main :
Java code
Package me.hatter.test.main; Import Org.springframework.context.ApplicationContext; Import Org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {public static void Main (string[] args) { String xml = "Classpath:me/hatter/test/main/test.xml "; ApplicationContext context = new Classpathxmlapplicationcontext (new string[] {XML}); System.out.println (Context.getbean ("Testcustom")); } }
The above example output is:
Testbean [name= This is a test custom tag]
Reproduced The principle of spring custom labels