栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring源码(七):自定义标签

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring源码(七):自定义标签

一.   Spring源码(六):深入自定义标签解析介绍了Spring对自定义标签的解析,这篇文件主要介绍如何自定义标签来完成复杂的业务需求。

二.  先简单回顾一下上篇文件介绍的自定义标签的解析。

        1.  获取所有的NamespaceHandler:

             通过Spring的SPI加载所有jar包meta-INF/spring.handlers下的文件,经过反射创建实例化并调用init初始化方法初始化BeanDefinitionParser解析器存入属性parsers中,然后将NamespaceHandler封装到handlerMappings属性中。            

  

        2. 根据namespaceUri(http://oss.housihai.com/schema/reyco)获取到具体的NamespaceHandler,然后调用它的parse()方法。

         3.在parse()方法中先根据标签到parsers属性中获取到具体的BeanDefinitionParser解析类,然后调用BeanDefinitionParser解析类的parse。在parse方法中将标签属性封装到BeanDefinition中,然后注册到Spring容器中。(parsers属性就是我们调用NamespaceHandler的init方法的时候注入的)

   三.  开始自定义标签:

         模拟bean标签注册Bean对象,标签样式如下图:

                id:bean的名称      class:beanClass的类型

        1.  定义meta-INF/spring-reyco.xsd文件




	
		
			
			
		
	
	


       2. 定义一个命名空间处理器NamespaceHandler,继承NamespaceHandlerSupport并重写init方法。

public class ReyconamespaceHandler extends NamespaceHandlerSupport {

	@Override
	public void init() {
		registerBeanDefinitionParser("reyco", new ReycoBeanDefinitionParser());
	}

}

       3.在init方法中需要注册一个BeanDefinitionParser(Bean定义解析器),解析我们的自定义标签。所以需要定义一个BeanDefinitionParser,实现parse方法。

public class ReycoBeanDefinitionParser implements BeanDefinitionParser {

	@Override
	public BeanDefinition parse(Element element, ParserContext parserContext) {
		String beanClassName = element.getAttribute("id");
		String beanClass = element.getAttribute("class");
		BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition();
		AbstractBeanDefinition beanDefinition = builder.getRawBeanDefinition();
		try {
			beanDefinition.setBeanClassName(beanClassName);
			beanDefinition.setBeanClass(Class.forName(beanClass));
			BeanDefinitionHolder beanDefinitionHolder = new BeanDefinitionHolder(beanDefinition,beanClassName, null);
			//注册BeanDefinition对象
			BeanDefinitionReaderUtils.registerBeanDefinition(beanDefinitionHolder, parserContext.getRegistry());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return beanDefinition;
	}
}

       4.编写meta-INF/spring.handlers文件,指定我们NamespaceHandler

http://oss.housihai.com/schema/reyco=com.reyco.tag.core.handlers.ReycoNamespaceHandler

       5. 编写meta-INF/spring.schemas文件

http://oss.housihai.com/schema/reyco.xsd=meta-INF/spring-reyco.xsd

        6. 编写配置文件  



        
 	
 	

        7. 让spring可以加载写的配置文件


@SpringBootApplication
@importResource(locations = { "classpath:spring.xml" })
public class TagApplication {
	
	public static void main(String[] args) {
		SpringApplication.run(TagApplication.class, args);
	}
	
}

           8.  测试是否可以正常使用,看下图,可以正常打印test说明我们的自定义标签起作用了!

               最后可以看一下整个项目的目录结构     

至此,整个Spring自定义标签已经介绍完了...

         源码地址https://github.com/sihaihou/customTag

         Spring源码(一):深入BeanPostProcessor源码—1

         Spring源码(二):深入BeanPostProcessor源码—2

         Spring源码(三):深入Spring Bean的生命周期

         Spring源码(四):深入Spring事件监听器

         Spring源码(五):深入Spring事务

         Spring源码(六):深入自定义标签解析

         Spring源码(七):自定义标签

         

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/592078.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号