广告
返回顶部
首页 > 资讯 > 后端开发 > Python >通过Spring自定义NamespaceHandler实现命名空间解析(推荐)
  • 697
分享到

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

2024-04-02 19:04:59 697人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

spring中在使用xml进行bean配置时,我们经常出现<context:annotation-config/>这样的配置,或是在使用dubbo时,暴露服务时,使用&l

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目录下

在这里插入图片描述

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

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

到此这篇关于通过Spring自定义NamespaceHandler实现命名空间解析的文章就介绍到这了,更多相关Spring自定义命名空间解析内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: 通过Spring自定义NamespaceHandler实现命名空间解析(推荐)

本文链接: https://www.lsjlt.com/news/123583.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作