Spring 自定义标签

Spring 自定义标签

步骤:

  1. 编写java bean

  2. 编写xsd配置文件

  3. 编写spring.handlers和spring.schmas

  4. 编写applicationContext.xml

  5. 编写NamespaceHandler和BeanDefinitionParser

项目目录结构

步骤

1. 编写java bean

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.zsr.spring.schema;
public class User {
private String id;
private String name;
private String sex;
private int age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}

2. 定义一个XSD文件

META-INF/user-1.0.xsd文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://zsr.github.io/schema/test"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tool="http://www.springframework.org/schema/tool" xmlns:beans="http://www.springframework.org/schema/beans"
targetNamespace="http://zsr.github.io/schema/test">
<xsd:import namespace="http://www.springframework.org/schema/beans" />
<xsd:import namespace="http://www.springframework.org/schema/tool" />
<xsd:annotation>
<xsd:documentation><![CDATA[ Namespace support for the test]]></xsd:documentation>
</xsd:annotation>
<xsd:element name="user">
<xsd:complexType>
<xsd:attribute name="id" type="xsd:string" use="required">
<xsd:annotation>
<xsd:documentation>
<![CDATA[
Unique id
]]>
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="name" type="xsd:string">
<xsd:annotation>
<xsd:documentation>姓名</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="sex" type="xsd:string">
<xsd:annotation>
<xsd:documentation>性别</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="age" type="xsd:int">
<xsd:annotation>
<xsd:documentation>年龄</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>
</xsd:element>
</xsd:schema>

3. 新建spring.handlers, spring.shcemas

在项目的META-INF目录下新建两个文件spring.handlers,和spring.shcemas.

  • Spring.handlers

在类org.springframework.beans.factory.xml.DefaultNamespaceHandlerResolver中已经写死了要取mapping的handlerMappingsLocation的路径:

1
public static finalString DEFAULT_HANDLER_MAPPINGS_LOCATION ="META-INF/spring.handlers";

spring.handlers内容:

1
http\://www.zsr.com/schema/test=com.zsr.spring.schema.MyNamespaceHandler
  • Spring.Schemas

在org.springframework.beans.factory.xml.PluggableSchemaResolver这个类中
同样写死了位置:

1
public static finalString DEFAULT_SCHEMA_MAPPINGS_LOCATION = "META-INF/spring.schemas";

spring.schemas内容:

1
http\://www.zsr.com/schema/test/user-1.0.xsd=META-INF/user-1.0.xsd

初始化的时候第一次调用的时候会调用:

1
PropertiesLoaderUtils.loadAllProperties(this.schemaMappingsLocation,this.classLoader)

把所有的文件名包含的取出来放入map中。

4. 编写applicationContext.xml

1
2
3
4
5
6
7
8
9
10
11
<?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://www.zsr.com/schema/test"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.zsr.com/schema/test
http://www.zsr.com/schema/test/user-1.0.xsd">
<test:user id="zsr" name="zsr" sex="male" age="24" />
</beans>

5. 编写NamespaceHandler和BeanDefinitionParser

  • NamespaceHandler
1
2
3
4
5
6
7
8
9
10
11
12
package com.zsr.spring.schema;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
public class MyNamespaceHandler extends NamespaceHandlerSupport {
@Override
public void init() {
registerBeanDefinitionParser("user", new UserBeanDefinitionParser());
}
}
  • BeanDefinitionParser
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.zsr.spring.schema;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.w3c.dom.Element;
public class UserBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
protected static final String USER_ID = "id";
protected static final String USER_NAME = "name";
protected static final String USER_AGE = "age";
protected static final String USER_SEX = "sex";
@Override
protected Class<?> getBeanClass(Element element) {
return User.class;
}
@Override
protected void doParse(Element element, BeanDefinitionBuilder builder) {
String id = element.getAttribute(USER_ID);
String name = element.getAttribute(USER_NAME);
String sex = element.getAttribute(USER_SEX);
int age = Integer.parseInt(element.getAttribute(USER_AGE));
builder.addPropertyValue(USER_ID, id);
builder.addPropertyValue(USER_NAME, name);
builder.addPropertyValue(USER_SEX, sex);
builder.addPropertyValue(USER_AGE, age);
}
}

热评文章