"Project Engineering"
"Person.java Model Class"
PackageCom.spring.selfxml.model;/*** Created by Higgincui on 2018/9/9.*/ Public classPerson {Private intAge ; PrivateString name; Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } @Override PublicString toString () {return"person{" + "age=" + Age + ", name= '" + name + ' \ ' + '} '; }}
"Custom person.xsd File"
<Xsd:schemaxmlns= "Http://higgin.com/schema"xmlns:xsd= "Http://www.w3.org/2001/XMLSchema"targetnamespace= "Http://higgin.com/schema"> <Xsd:complextypename= "complexType1"> <Xsd:attributename= "Name"type= "Xsd:string"> <xsd:annotation> <xsd:documentation><![cdata[the elementname1 name.]></xsd:documentation> </xsd:annotation> </Xsd:attribute> <Xsd:attributename= "Age"type= "Xsd:int"> <xsd:annotation> <xsd:documentation><![cdata[the elementname1 age.]></xsd:documentation> </xsd:annotation> </Xsd:attribute> </Xsd:complextype> <xsd:elementname= "elementname1"type= "complexType1"> <xsd:annotation> <xsd:documentation><![cdata[ elementname1 's documentation]]></xsd:documentation> </xsd:annotation> </xsd:element></Xsd:schema>
Description
1. Define targetnamespace= "Http://higgin.com/schema", targetnamespace represents the target namespace, and the value of xmlns is the same as this
2.xsd:element defines the elements that will be used in the XML file, such as the application in <dubbo:application>.
3.xsd:attribute defines the properties in the model, such as the name in <dubbo:application name= "xxx" >, and can specify the type of the attribute, and thus the role of detection
"Spring.schemas file"
Role: The file is used to specify the location of the XSD file
http\:// higgin.com/schema/person.xsd=meta-inf/person.xsd
The http://http/://higgin.com/schema/ in the Spring.schemas file is the same as the targetname in the Person.xsd file.
"Personbeandefinitionparser.java Parser"
Function: Used to parse the custom XML tag
PackageCom.spring.selfxml.shcema;Importorg.springframework.beans.factory.config.BeanDefinition;ImportOrg.springframework.beans.factory.support.BeanDefinitionRegistry;Importorg.springframework.beans.factory.support.RootBeanDefinition;ImportOrg.springframework.beans.factory.xml.BeanDefinitionParser;ImportOrg.springframework.beans.factory.xml.ParserContext;Importorg.w3c.dom.Element;/*** Created by Higgincui on 2018/9/9.*/ Public classPersonbeandefinitionparserImplementsbeandefinitionparser{Private FinalClass<?>Beanclass; PublicPersonbeandefinitionparser (class<?>Beanclass) { This. Beanclass =Beanclass; } PublicBeandefinition Parse (element element, ParserContext parsercontext) {rootbeandefinition beandefinition=Newrootbeandefinition (); Beandefinition.setbeanclass (Beanclass); Beandefinition.setlazyinit (false); Beandefinition.getpropertyvalues (). Add ("Name", Element.getattribute ("name")); Beandefinition.getpropertyvalues (). Add (' Age ', Element.getattribute ("Age")); Beandefinitionregistry Beandefinitionregistry=Parsercontext.getregistry (); //registering beans in beandefinitionregistrybeandefinitionregistry.registerbeandefinition (Beanclass.getname (), beandefinition); returnbeandefinition; }}
"Personnamespacehandler.java namespace Processor"
Function: Mainly used to register beandefinition parser.
PackageCom.spring.selfxml.shcema;ImportCom.spring.selfxml.model.Person;ImportOrg.springframework.beans.factory.xml.NamespaceHandlerSupport;/*** Created by Higgincui on 2018/9/9.*/ Public classPersonnamespacehandlerextendsNamespacehandlersupport { Public voidinit () {Registerbeandefinitionparser ("Elementname1",NewPersonbeandefinitionparser (person.class)); }}
Description: A beandefinitionparser is usually registered for each xsd:element.
"Spring.handlers file"
Role: Used to correlate namespaces in processors Personnamespacehandler and person.xsd targetnamespace
Http\://higgin.com/schema=com.spring.selfxml.shcema.personnamespacehandler
Description: The key in the Red section here is the targetnamespace in the XSD file
"Person.xml"
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:person= "Http://higgin.com/schema"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd Http://higgin.com/schema http://higgin.com/schema/person.xsd "> <person:elementname1name= "Zhangsan" Age= "+"/></Beans>
Description
1. The value of Xmlns:person is targetnamespace in the Person.xml file
2. Xmlns:person can be written xmlns:xxx, at this time <person:elementname1 ..../> will be changed to <xxx:elementname1 ....>
"Test.java"
PackageCom.spring.selfxml;ImportCom.spring.selfxml.model.Person;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;/*** Created by Higgincui on 2018/9/9.*/ Public classTest { Public Static voidMain (string[] args) {ApplicationContext context=NewClasspathxmlapplicationcontext ("Person.xml"); Person Person= (person) context.getbean (person).class. GetName ()); System.out.println (Person.tostring ()); }}
"Run Results"
Summary
The process of spring custom XML code:
1. Use Resourceloader to load the configuration file XML as a resource object
2. Use Beandefinitionreader to parse the configuration information: parse each <bean> into a Beandefinition object and store it in Beandefinitionregistry. In fact, Beandefinitionreader calls Beandefinitionparser for parsing, and after parsing is completed, it is registered to Beandefinitionregistry.
08_spring Custom Labels