好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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

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

?

1

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

1、定义bean

?

1

2

3

4

5

6

7

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实现如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

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());

         // 注入其他解析器...

     }

}

解析器代码:

?

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

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\://HdhCmsTestlcl测试数据/schema/custom=com.lcl.spring.ext.customnamespacehandler

4、编写xsd文件

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

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

5、编写spring.schemas

http\://HdhCmsTestlcl测试数据/schema/custom.xsd=custom-beans-4.3.xsd

6、编写spring配置文件

?

1

2

3

4

5

6

7

8

<?xml version= "1.0" encoding= "utf-8" ?>

<beans xmlns= "http://HdhCmsTestspringframework.org/schema/beans"

        xmlns:xsi= "http://HdhCmsTestw3.org/2001/xmlschema-instance"

        xmlns:custom= "http://HdhCmsTestlcl测试数据/schema/custom"

        xsi:schemalocation="http: //HdhCmsTestspringframework.org/schema/beans http://HdhCmsTestspringframework.org/schema/beans/spring-beans.xsd

             http: //HdhCmsTestlcl测试数据/schema/custom http://HdhCmsTestlcl测试数据/schema/custom.xsd">

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

</beans>

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

7、测试

?

1

2

3

4

5

6

7

8

9

10

11

12

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自定义命名空间解析内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/DoBetterEveryDaY/article/details/115679589

查看更多关于通过Spring自定义NamespaceHandler实现命名空间解析(推荐)的详细内容...

  阅读:21次