通过Spring自定义NamespaceHandler实现命名空间解析(推荐)

5 篇文章 0 订阅

通过Spring自定义NamespaceHandler实现命名空间解析(推荐)

原文链接:通过Spring自定义NamespaceHandler实现命名空间解析(推荐)_java_脚本之家 (jb51.net)

NamespaceHandler 接口,DefaultBeanDefinitionDocumentReader 使用该接口来处理在spring xml 配置文件中自定义的命名空间

  • [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CyzFQxUV-1666232624897)(../../思维导图关联文件/markdownImg/image-20221020102311922.png)]

这篇文章主要介绍了通过Spring自定义NamespaceHandler实现命名空间解析,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

spring中在使用xml进行bean配置时,我们经常出现<context:annotation-config/>这样的配置,或是在使用dubbo时,暴露服务时,使用<dubbo:service interface="xxx" ref="yyy" />,我们知道仅仅通过这些简单的配置,其实完成了很多工作,那么我们能不能也实现这种功能,仅通过简单的配置,实现bean定义加载的过程中细节的隐藏,但完成复杂的功能呢?
答案是肯定的,方法是我们使用自定义NamespaceHandler进行处理,具体步骤如下:
说明:下面模拟spring bean定义功能,使用我们自定义的方式来实现CustomBean注入到Spring容器中,具体用法如下:

<custom:bean class="com.lcl.spring.beans.CustomBean"></custom:bean>

1、定义Bean

package com.lcl.spring.beans;

public class CustomBean {
    public void sayHi(){
        System.out.println("Hello Custom NamespaceHandler");
    }
}

我们想把这个类通过xml方式注入到spring容器中

2、编写自定义NamespaceHandler

NamespaceHandler的功能就是解析我们自定义的custom命名空间的,为了方便起见,我们实现NamespaceHandlerSupport,其内部通过BeanDefinitionParser对具体的标签进行处理,即对我们定义的<custom:bean/>进行具体处理。

NamespaceHandler实现如下:

package com.lcl.spring.ext;

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

/**
 * 自定义命名空间解析器
 */
public class CustomNamespaceHandler extends NamespaceHandlerSupport {

    @Override
    public void init() {
        // 在初始化方法中,注入标签解析器,此时我们需要解析<custom:bean/>
        registerBeanDefinitionParser("bean", new CustomBeanDefinitionParser());
        // 注入其他解析器...
    }
}

解析器代码:

package com.lcl.spring.ext;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;

/**
 * 自定义解析器
 */
public class CustomBeanDefinitionParser implements BeanDefinitionParser {

    @Override
    public BeanDefinition parse(Element element, ParserContext parserContext) {
        // 为了演示方便起见,使用BeanDefinitionParserDelegate解析bean definition
        BeanDefinitionHolder beanDefinitionHolder = parserContext.getDelegate().parseBeanDefinitionElement(element);
        // 使用工具类注册
        BeanDefinitionReaderUtils.registerBeanDefinition(beanDefinitionHolder, parserContext.getRegistry());
        // 或是使用注册器注册
        //parserContext.getRegistry().registerBeanDefinition(beanDefinitionHolder.getBeanName(),beanDefinitionHolder.getBeanDefinition());
        return beanDefinitionHolder.getBeanDefinition();
    }
}

特别说明:在解析器中执行parse返回BeanDefinition并不会实现被返回的bean定义被自动注册到spring容器中,需要自己手工的执行注册中,如执行上述代码中的BeanDefinitionReaderUtils.registerBeanDefinition或使用parserContext.getRegistry().registerBeanDefinition

另外为了更方便的处理解析过程,解析器可以实现AbstractSingleBeanDefinitionParser

3、编写spring.handlers文件

这个是核心的配置文件,定义了具体handler处理器,其实spring内部对context、aop、task等命名空间,也是通过此方式进行处理的。
具体内容如下:

http://www.lcl.com/schema/custom=com.lcl.spring.ext.CustomNamespaceHandler

4、编写XSD文件

为了简单期间,我直接使用spring-beans-4.3.xsd文件修改,命名为custom-beans-4.3.xsd,放置在resources目录下

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0vdk89tE-1666232537125)(D:\all\思维导图\思维导图关联文件\markdownImg\2021041414381960.png)]

注意:需要修改如图所示部分

5、编写spring.schemas

http://www.lcl.com/schema/custom.xsd=custom-beans-4.3.xsd

6、编写spring配置文件

<?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:custom="http://www.lcl.com/schema/custom"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.lcl.com/schema/custom http://www.lcl.com/schema/custom.xsd">
    <custom:bean class="com.lcl.spring.beans.CustomBean"></custom:bean>
</beans>

使用了我们自定义的<custom:bean/>标签进行定义

7、测试

package com.lcl.spring;

import com.lcl.spring.beans.CustomBean;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringCustomNamespaceHandlerDemo {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("context.xml");
        CustomBean bean = applicationContext.getBean(CustomBean.class);
        bean.sayHi();
    }
}

输入结果如下:

Hello Custom NamespaceHandler

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值