I. Introduction
I recorded an article about spring default label resolution and registration implementation.
This time record custom tags to combine with that article to deepen understanding
Second, the text
First of all, let's take a look at how to customize a spring label and use it like a bean label. Let's first overview some of the overall process:
- Create a component that needs to be extended
- Define an XSD file to describe the content of the component
- Create a java class that implements the BeanDefinitionParser interface to parse the definitions and component definitions in the XSD file
- Create a Handler class, extend
NameSpaceHandlerSupport
, the purpose is to register the component to the container. - Write (add)
Spring.handlers和Spring.schemas
files.
After completing the above work, then we can use our custom label.
1. Create a pojo class
public class User {
private String name;
private String sex;
private String email;
private String id;
//set get method...
}
2. Define an xsd file to describe the component class content
In the past, spring used dtd files, but now almost all of them are used xsd files. If you don’t know the definition of xsd files, you can go to http://www.phpstudy.net/e/schema/ to have a look at this website. Comparison of usage Simple, I won’t introduce how to use it here. The following is to define an xsd file according to the rules of the schema.
<?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="name" type="string" />
<attribute name="sex" type="string" />
<attribute name="email" type="string" />
</complexType>
</element>
</schema>
3. Implement the AbstractSingleBeanDefinitionParser interface
After implementing this interface, when Spring loads the document and encounters the tag you defined, it will call back your parsing method to perform your custom attribute parsing.
public class UserBeanDefinitionParser extends AbstractSingleBeanDefinitionParser{
@Override
protected Class<?> getBeanClass(Element element) {
return User.class;
}
//从elelment中解析并提取对应的元素
@Override
protected void doParse(Element element, BeanDefinitionBuilder builder) {
String name=element.getAttribute("name");
String email=element.getAttribute("sex");
String sex=element.getAttribute("sex");
//将提取到的数据放入beanDefinitionBuilder 中,待完成所有的bean解析后统一放到beanfactory
if(StringUtils.hasText(name)){
builder.addPropertyValue("name", name);
}
if(StringUtils.hasText(email)){
builder.addPropertyValue("email", email);
}
if(StringUtils.hasText(sex)){
builder.addPropertyValue("sex", sex);
}
}
}
4. Inherit the abstract class NamespaceHandlerSupport
The tag is defined, here is the tag processor, the processor will (UserBeanDefinitionParser)
pass the instance of the parsing class to the spring, so that when the spring parses the tag, the method of the instance can be called back.
public class MyNamespaceHandler extends NamespaceHandlerSupport {
@Override
public void init() {
registerBeanDefinitionParser("user", new UserBeanDefinitionParser());
}
}
5. Modify (add) spring.handlers file and spring.schemas file
The default path of these two files is under the path of META-INF of spring-aop
http\://www.lexueba.com/schema/user
(Customized)=net.itaem.handler.MyNamespaceHandler
Modify the content of the spring.schemas file and add the following
http\://www.lexueba.com/schema/user.xsd
(Customized)=org/springframework/beans/factory/xml/use.xsd
After modification, when using custom tags, these will be added to xsi:schemaLocation, and then spring will find the xsd file and handler by itself.
6. Create a test configuration file
When using namespace can add their own definitions, and then xsi:schemaLocation
add the corresponding content, you can use your own definition of the label.
<?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:myname="http://www.lexueba.com/schema/user"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.lexueba.com/schema/user http://www.lexueba.com/schema/user.xsd">
<myname:user id="beantest" name="whx" email="[email protected]" sex="男"/>
</beans>
7. Write test code
public class Test {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("net/itaem/source/custom_user.xml");
User bt=(User) context.getBean("beantest");
System.out.println(bt);
}
}