Spring自定义标签的使用

为什么自定义标签

自定义标签是spring为了给开发人员扩展组件使用的,因为它提供了一个标准的公共可插拔的接口;目前我们都知道spring非常强大,不过实际上除了spring-core和spring-beans外,其他都是通过自定义标签扩展实现的,其次还有一些开源组件也是,如dubbo等。所以,对于想扩展spring组件的小伙伴来说,了解如何自定义标签和相应的原理是必须走的第一步。

自定义标签

按照spring加载、解析的顺序有以下五个基本文件:
* spring.schemas
* *.xsd
* spring.handlers
* Handler
* Parser
* 最后引入到配置文件中使用。
* 基本自定义标签需要实现两个关键接口:
NamespaceHandlerSupport
BeanDefinitionParser

工程机构:


pom依赖:

1
2
3
4
5
6
7
8
9
10
11
12
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
</dependencies>
XML

spring.schemas

spring.schemas是告诉spring容器我们的自定义的xsd文件在哪个地方。

1
http\://www.leehao.com/schema/user.xsd=META-INF/user.xsd
XML

类似与key=value的形式,如果找不到会去网络上下载。

*.xsd

DTD与XSD是spring中最常见的基础配置文件,打个比方DTD类似于Java的关键字,而XSD则类似于Java的语法。
而我们自定义XSD文件则是对于我们定义bean对象时可以使用的属性限制或者说支持的那些属性配置,自定义的user.xsd如下。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="utf-8" ?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.leehao.com/schema/user"
xmlns:tns="http://www.leehao.com/schema/user"
elementFormDefault="qualified">
<element name="user">
<complexType>
<attribute name="id" type="string"/>
<attribute name="userName" type="string"/>
<attribute name="age" type="int"/>
</complexType>
</element>

</schema>
XSD

指定namespace时,请勿使用spring默认前缀。
这里我们定义了一个element属性可以设置id,userName,age.

Handlers

Handlers作为自定义标签解析的入口需要在默认文件名称spring.handlers指定,文件内容如下:
这里是我们自定义的MyNamespaceHandler.目的是为了注册我们对应标签的解析类;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.leehao.handler;

import com.leehao.parser.UserBeanDefinitionParser;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;

/**
* @ClassName MyNamespaceHandler
* @Description MyNamespaceHandler
* @Author lihao
* @Date 2020/4/23 11:13
* @Version 1.0
**/
public class MyNamespaceHandler extends NamespaceHandlerSupport {
public void init() {
registerBeanDefinitionParser("user", new UserBeanDefinitionParser());
}
}
JAVA

Parser

这里我们不直接实现这个接口BeanDefinitionParser,我们直接使用他的抽象实现类AbstractSingleBeanDefinitionParser。这里我们获取属性并设置到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
package com.leehao.parser;

import com.leehao.model.User;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;

/**
* @ClassName UserBeanDefinitionParser
* @Description UserBeanDefinitionParser
* @Author lihao
* @Date 2020/4/23 11:08
* @Version 1.0
**/
public class UserBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
@Override
protected Class<?> getBeanClass(Element element) {
return User.class;
}

@Override
protected void doParse(Element element, BeanDefinitionBuilder builder) {
String userName = element.getAttribute("userName");
String age = element.getAttribute("age");
if (StringUtils.hasText(userName)) {
builder.addPropertyValue("userName", userName);
}
if (StringUtils.hasText(age)) {
builder.addPropertyValue("age", Integer.valueOf(age));
}
}
}
JAVA

自定义标签

test.xml

1
2
3
4
5
6
7
8
9
<?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:leehao="http://www.leehao.com/schema/user"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.leehao.com/schema/user http://www.leehao.com/schema/user.xsd">

<leehao:user id = "userBean" userName = "leehao" age="27"/>
</beans>
XML

使用自定义标签

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.leehao;

import com.leehao.model.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* @ClassName App
* @Description App
* @Author lihao
* @Date 2020/4/14 09:39
* @Version 1.0
**/
public class App {
public static void main(String[] args) {
ApplicationContext beans=new ClassPathXmlApplicationContext("classpath:test.xml");
User user=(User)beans.getBean("userBean");
System.out.println("username:"+user.getUserName()+" "+"age:"+user.getAge());
}
}
JAVA

执行上述程序后,输出:

1
2
3
username:leehao  age:27

Process finished with exit code 0
AWK

致谢

https://blog.csdn.net/zzg1229059735/article/details/82669955
https://mp.weixin.qq.com/s/B7KynNoEZRIsIjTDRYw1mQ


Spring自定义标签的使用
https://leehoward.cn/2020/04/23/Spring自定义标签的使用/
作者
lihao
发布于
2020年4月23日
许可协议