004-Spring-BeanDefinition的拼装(自定义标签解析)

扩展 Spring 自定义标签配置一般需要以下几个步骤:

  1、创建一个需要扩展的组件。

  2、定义一个 XSD 文件,用于描述组件内容。需要上传至一个可访问站点

  3、创建一个实现 org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser 接口的类,用来解析 XSD 文件中的定义和组件定义。

  4、创建一个 Handler,继承 org.springframework.beans.factory.xml.NamespaceHandlerSupport 抽象类 ,用于将组件注册到 Spring 容器。

  5、编写 spring.handlers 和 Spring.schemas 文件。需要打包进META-INF

1、代码结构

  

2、创建普通java bean

package com.github.bjlhx15.common.beandefinition;

public class CommonUser {
    private String id;
    private String userName;
    private String email;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}
View Code

3、XSD定义

对这个 Java Bean 进行了描述外,还定义了 xmlns="" 和 targetNamespace="" 这两个值

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns="http://blog.bjlhx.top/other/CommonUser"
            targetNamespace="http://blog.bjlhx.top/other/CommonUser"
            elementFormDefault="qualified">

    <xsd:element name="commonuser">
        <xsd:complexType>
            <xsd:attribute name="id" type="xsd:string"/>
            <xsd:attribute name="userName" type="xsd:string"/>
            <xsd:attribute name="email" type="xsd:string"/>
        </xsd:complexType>
    </xsd:element>

</xsd:schema>

同时将文件:CommonUser.xsd 上传至 http://blog.bjlhx.top/other/  路径下

4、定义Parser类

定义一个 Parser 类,该类继承 AbstractSingleBeanDefinitionParser ,并实现 #getBeanClass(Element element) 和 #doParse(Element element, BeanDefinitionBuilder builder) 两个方法。主要是用于解析 XSD 文件中的定义和组件定义

package com.github.bjlhx15.common.beandefinition;

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

//定义一个 Parser 类,该类继承 AbstractSingleBeanDefinitionParser
//用于解析 XSD 文件中的定义和组件定义
public class CommonUserDefinitionParser extends AbstractSingleBeanDefinitionParser {

    @Override
    protected Class<?> getBeanClass(Element element) {
        return CommonUser.class;
    }

    @Override
    protected void doParse(Element element, BeanDefinitionBuilder builder) {
        String id = element.getAttribute("id");
        String userName = element.getAttribute("userName");
        String email = element.getAttribute("email");

        if (StringUtils.hasText(id)) {
            builder.addPropertyValue("id", id);
        }

        if (StringUtils.hasText(userName)) {
            builder.addPropertyValue("userName", userName);
        }

        if (StringUtils.hasText(email)) {
            builder.addPropertyValue("email", email);
        }
    }
}
View Code

5、定义NamespaceHandler类

定义 NamespaceHandler 类,继承 NamespaceHandlerSupport ,主要目的是将组件注册到 Spring 容器中。

package com.github.bjlhx15.common.beandefinition;

import org.springframework.beans.factory.xml.NamespaceHandlerSupport;

public class CommonUserNamespaceHandler extends NamespaceHandlerSupport {


    @Override
    public void init() {
        //将组件注册到 Spring 容器中。
        registerBeanDefinitionParser("commonuser", new CommonUserDefinitionParser());
    }
}
View Code

6、定义配置

spring.handlers 文件、

http\://blog.bjlhx.top/other/CommonUser=com.github.bjlhx15.common.beandefinition.CommonUserNamespaceHandler

定义 Spring.schemas 文件

http://blog.bjlhx.top/other/CommonUser.xsd=CommonUser.xsd

7、使用

<?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:myTag="http://blog.bjlhx.top/other/CommonUser"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
        http://blog.bjlhx.top/other/CommonUser
         http://blog.bjlhx.top/other/CommonUser.xsd">

    <myTag:commonuser id="user" email="bjlhx15@163.com" userName="lihongxu" />

</beans>

java代码

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        CommonUser user = (CommonUser) context.getBean("user");
        System.out.println(user.getUserName() + "----" + user.getEmail());
    }

代码地址:https://github.com/bjlhx15/common.git  spring-demo-001-beandefinition/beandefinition-001

 

  

 

 

参看:https://blog.csdn.net/qq330983778/article/details/88544575

 

posted @ 2020-04-28 14:24  bjlhx15  阅读(402)  评论(0)    收藏  举报
Copyright ©2011~2020 JD-李宏旭