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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

DoBetterEveryDay   2021-04-14 我要评论
想了解通过Spring自定义NamespaceHandler实现命名空间解析(推荐)的相关内容吗,DoBetterEveryDay在本文为您仔细讲解Spring自定义命名空间解析的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Spring自定义命名空间解析,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目录下

在这里插入图片描述

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

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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们