通過Spring自定義NamespaceHandler實(shí)現(xiàn)命名空間解析(推薦)
spring中在使用xml進(jìn)行bean配置時(shí),我們經(jīng)常出現(xiàn)<context:annotation-config/>
這樣的配置,或是在使用dubbo時(shí),暴露服務(wù)時(shí),使用<dubbo:service interface="xxx" ref="yyy" />
,我們知道僅僅通過這些簡(jiǎn)單的配置,其實(shí)完成了很多工作,那么我們能不能也實(shí)現(xiàn)這種功能,僅通過簡(jiǎn)單的配置,實(shí)現(xiàn)bean定義加載的過程中細(xì)節(jié)的隱藏,但完成復(fù)雜的功能呢?
答案是肯定的,方法是我們使用自定義NamespaceHandler進(jìn)行處理,具體步驟如下:
說明:下面模擬spring bean定義功能,使用我們自定義的方式來實(shí)現(xiàn)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"); } }
我們想把這個(gè)類通過xml方式注入到spring容器中
2、編寫自定義NamespaceHandler
NamespaceHandler的功能就是解析我們自定義的custom命名空間的,為了方便起見,我們實(shí)現(xiàn)NamespaceHandlerSupport,其內(nèi)部通過BeanDefinitionParser對(duì)具體的標(biāo)簽進(jìn)行處理,即對(duì)我們定義的<custom:bean/>
進(jìn)行具體處理。
NamespaceHandler實(shí)現(xiàn)如下:
package com.lcl.spring.ext; import org.springframework.beans.factory.xml.NamespaceHandlerSupport; /** * 自定義命名空間解析器 */ public class CustomNamespaceHandler extends NamespaceHandlerSupport { @Override public void init() { // 在初始化方法中,注入標(biāo)簽解析器,此時(shí)我們需要解析<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); // 使用工具類注冊(cè) BeanDefinitionReaderUtils.registerBeanDefinition(beanDefinitionHolder, parserContext.getRegistry()); // 或是使用注冊(cè)器注冊(cè) //parserContext.getRegistry().registerBeanDefinition(beanDefinitionHolder.getBeanName(),beanDefinitionHolder.getBeanDefinition()); return beanDefinitionHolder.getBeanDefinition(); } }
特別說明:在解析器中執(zhí)行parse返回BeanDefinition并不會(huì)實(shí)現(xiàn)被返回的bean定義被自動(dòng)注冊(cè)到spring容器中,需要自己手工的執(zhí)行注冊(cè)中,如執(zhí)行上述代碼中的BeanDefinitionReaderUtils.registerBeanDefinition或使用parserContext.getRegistry().registerBeanDefinition
另外為了更方便的處理解析過程,解析器可以實(shí)現(xiàn)AbstractSingleBeanDefinitionParser
3、編寫spring.handlers文件
這個(gè)是核心的配置文件,定義了具體handler處理器,其實(shí)spring內(nèi)部對(duì)context、aop、task等命名空間,也是通過此方式進(jìn)行處理的。
具體內(nèi)容如下:
http\://www.lcl.com/schema/custom=com.lcl.spring.ext.CustomNamespaceHandler
4、編寫XSD文件
為了簡(jiǎn)單期間,我直接使用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/>
標(biāo)簽進(jìn)行定義
7、測(cè)試
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(); } }
輸入結(jié)果如下:
Hello Custom NamespaceHandler
到此這篇關(guān)于通過Spring自定義NamespaceHandler實(shí)現(xiàn)命名空間解析的文章就介紹到這了,更多相關(guān)Spring自定義命名空間解析內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java實(shí)現(xiàn)mysql操作類分享 java連接mysql
這篇文章主要介紹了java實(shí)現(xiàn)的mysql操作類示例,大家在連接數(shù)據(jù)的時(shí)候可以直接使用了2014-01-01SpringMVC獲取請(qǐng)求參數(shù)的方法詳解
這篇文章主要為大家詳細(xì)介紹了SpringMVC中獲取請(qǐng)求參數(shù)的方法,例如通過ServletAPI獲取和通過控制器方法的形參獲取請(qǐng)求參數(shù)等,需要的可以參考下2023-07-07IDEA中Javaweb項(xiàng)目圖片加載不出來解決方案
在IDEA中能夠正常的預(yù)覽到圖片,但是在生成項(xiàng)目的war包時(shí),項(xiàng)目的目錄結(jié)構(gòu)卻會(huì)發(fā)生變化,所以無(wú)法訪問圖片,本文主要介紹了IDEA中Javaweb項(xiàng)目圖片加載不出來解決方案,感興趣的可以了解一下2023-10-10JDK20?+?SpringBoot?3.1.0?+?JdbcTemplate?使用案例詳解
通過 JdbcTemplate 直接執(zhí)行 SQL 語(yǔ)句,結(jié)合源碼動(dòng)態(tài)編譯即可方便實(shí)現(xiàn)動(dòng)態(tài)修改代碼邏輯的效果,這篇文章主要介紹了JDK20?+?SpringBoot?3.1.0?+?JdbcTemplate?使用,需要的朋友可以參考下2023-09-09springboot+thymeleaf+layui的實(shí)現(xiàn)示例
本文主要介紹了springboot+thymeleaf+layui的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-12-12深入了解SpringBoot中@InitBinder注解的使用
這篇文章主要介紹了深入了解SpringBoot中@InitBinder注解的使用,@InitBinder注解可以作用在被@Controller注解的類的方法上,表示為當(dāng)前控制器注冊(cè)一個(gè)屬性編輯器,用于對(duì)WebDataBinder進(jìn)行初始化,且只對(duì)當(dāng)前的Controller有效,需要的朋友可以參考下2023-10-10Maven構(gòu)建Hadoop項(xiàng)目的實(shí)踐步驟
本文主要介紹了Maven構(gòu)建Hadoop項(xiàng)目的實(shí)踐步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06SpringCloud?Gateway詳細(xì)分析實(shí)現(xiàn)負(fù)載均衡與熔斷和限流
這篇文章主要介紹了SpringCloud?Gateway實(shí)現(xiàn)路由轉(zhuǎn)發(fā),負(fù)載均衡,熔斷和限流,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07