Because of the previous account, the phone binding problem, unable to operate, the new blog opened.
This time we analyze the custom label parsing.
After the conversion from the configuration file to document is completed and the corresponding root is extracted, all elements are parsed, and in this process the default label and custom label are started, and the two formats are differentiated. The functions are as follows:
protected void Parsebeandefinitions (Element root, beandefinitionparserdelegate delegate) {
if ( Delegate.isdefaultnamespace (Root) {
NodeList nl = root.getchildnodes ();
for (int i = 0;i<nl.getlength (); i++) {
node node = nl.item (i);
if (node instanceof element) {
element ele = (element) node;
if (Delegate.isdefaultnamespace (ele)) {
parsedefaultelement (ele, delegate);
} else{
delegate.parsecustomelement (ele);
}}} else {
delegate.parsecustomelement (root);
}
}
In the article, all the functions are centered around a sentence of code
Delegate.parsecustomelement (root); From the above function we can see that when spring gets an element, the first thing to do is to parse according to the namespace, if it is the default namespace,
The Parsedefaultelement method is used for element parsing, otherwise the Parsecustomelement method is used for parsing. Before we analyze the parsing of custom labels, let's look at the use of custom labels.
1. Use of custom tagsIn many cases, we need to provide configurable support for the system, which can be configured directly based on the Springde standard Bean, but it is awkward when the configuration is more complex or requires more rich control. The usual approach is to parse the well-defined XML file in a primitive way and then transform it into a configuration object. This approach can certainly solve all the problems, but the implementation is cumbersome, especially when the configuration is very complex, parsing is a burden to be considered. Spring provides extensible schema support, which is a good compromise, and it takes a few steps to extend the Spring Custom label configuration (provided the spring core package is added to the project). Creating a component that needs to be extended defines an XSD file that describes the component content, creates a file, implements the Beandefinitionparser interface, and creates a handler file that resolves the definition and component definitions in the XSD file. Extended from Namespacehandlersupport to register the component with the spring container. Writing spring.handlers and Spring.schmas files now let's follow the steps above to lead the reader through the process of customizing the label Step-by-step. (1) First we create an ordinary Pojo, this pojo nothing special, just to receive the configuration file.
public class User {
private String userName;
private String email;
}
(2) Define an XSD file to describe the component content.
<?xml version= "1.0" encoding= "UTF-8"?> <schema
xmlns= "Http://www.w3.org/2001/XMLSchema"
Targetnamespace= "Http://www.lexueba.com/schema/user"
xmlns:tns= "Http://www.lexueba.com/schema/user
" elementformdefault= "qualified" >
<element name= "user" >
<complexType>
<attribute >name= "id" type= "string" >
<attribute>name= "userName" type= "string" >
<attribute>name = "Email" type= "string" >
</complexType>
</element>
</schema>
A new targetnamespace is described in the XSD file above, and in this space a element,uer with the name user is defined with 3 properties id,username and email, where the email type is string. This three class is primarily used to verify the SPIRNG configuration file custom format. (3) Create a file that implements the Beandefinitionparser interface for parsing definitions and component definitions in XSD files.
Parse and extract the corresponding elements from element
protected void Doparse (element element, Beandefinitionbuilder Bean) {
String userName = Element.getattributes ("UserName");
String email = element.getattributes ("email");
Put the extracted data into the beandefinitionbuilder until all the beans have been parsed and registered uniformly into beanfactory
if (Stringutils.hastext (UserName)) {
Bean.addpropertyvalue ("UserName", userName);
}
if (stringutils.hastest (email)) {
bean.addpropertyvalue ("email", email);
}
Create a handler file, extended from Namespacehandlersupport, to register the component with the SPIRNG container.
public class Mynamespacehandler extends namespacehandlersupport{public
void init () {
Registerbeandefinitionparser ("User", new Userbeandefinitionparser ());
}
}
The above code is very simple, but when encountering a custom tag <user:aaa such as the beginning of the user-like element, it will throw this element to the corresponding userbeandefinitionparser to parse. (5) Write spirng.handlers and Spring.schemas files, the default location is under the/meta-inf/folder of the project.
Spring.handlers Http\://www.lexueba.com/schema/user=test.customtag.mynamespacehandler Spring.schemas http\:// Www.lexueba.com/Schema and User.xsd=meta-inf/spring-test.xsd Here, the custom configuration is over, The approximate process of spring load customization is to encounter custom tags and then go to Spring.handlers and spring.schemas to find the corresponding handler and XSD, the default location is/meta-inf/, In order to find the corresponding handler and parsing elements of the parser, thus completing the entire custom element resolution, that is, the custom and spring in the default standard configuration is different in the spring to the custom label resolution of the work entrusted to the user to implement. (6) Create a test configuration file, and after introducing the corresponding namespace and XSD in the configuration file, you can use the custom label directly.
<beans xmlns= "http://www. Springframeword.org/schema/beans "
xmlns:xsi=" Http://www.lexueba.com/schema/user "
xmlns:myname=" Http://www.lexueba.com/schema/user "
xsi:schemalocation=" http://www. Springframework.org/schema/beans http://www. Springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.lexueba.com/schema/user http://www.lexueba.com/schema/user.xsd>
<user:user id= "Testbean" Username= "AAA" email= "BBB"/>
</beans>