如果要开发一个spring自定义的标签比如:
<pre:strategy id="strategyFrist" interface="org.test.spring.schema.strategy.FristStrategy" url="tcp://strategy/frist" />
扩展Spring自定义标签大致需要如下几步:
- 创建需要扩展的组件
- 定义XSD文件描述组件内容
- 创建一个文件,实现BeanDefinitionParser接口,用来解析XSD文件中的定义和组件定义
- 创建Handler文件,扩展字NamespaceHandlerSupport,目的是将组件注册到Spring容器
- 编写Spring.handlers和Spring.schemas文件
具体步骤如下:
1、创建maven工程spring-schema,添加spring相应jar包
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> <optional>true</optional> </dependency>
2、创建一个bean,接收自定义标签属性,如StrategyBean,这里实现了FactoryBean接口,为了将自定义的标签解析完成后注入spring容器
public class StrategyBean implements FactoryBean<Object>{ private String id; private String url; private Class<?> interfaceName; @Override public Object getObject() throws Exception { return interfaceName.newInstance(); } @Override public Class<?> getObjectType() { return interfaceName; } @Override public boolean isSingleton() { return true; } //省略get/set方法 }
3、在工程的META-INF文件夹下定义一个XSD文件描述组件内容,如src/main/resources/META-INF/strategy.xsd
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.shijun.com/schema/strategy" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="strategy"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute type="xs:string" name="id" use="required"/> <xs:attribute type="xs:anySimpleType" name="interface" use="required"/> <xs:attribute type="xs:string" name="url" use="required"/> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> </xs:schema>
4、实现BeanDefinitionParser接口,用来解析XSD文件中的定义和组件定义,如StrategyBeanDefinitionParser
/** * 配置解析器 * @author shijun03 */ public class StrategyBeanDefinitionParser extends AbstractSingleBeanDefinitionParser{ @Override protected Class<?> getBeanClass(Element element) { return StrategyBean.class; } @Override protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { String id = element.getAttribute("id"); String url = element.getAttribute("url"); String interfaceName = element.getAttribute("interface"); if(StringUtils.hasText(id)){ //判断bean是否已经注册 boolean hasRegistry = parserContext.getRegistry().containsBeanDefinition(id); if(hasRegistry){ throw new IllegalArgumentException("bean has registry ,please check bean ,id = "+id); } builder.addPropertyValue("id", id); } if(StringUtils.hasText(url)){ builder.addPropertyValue("url", url); } if(StringUtils.hasText(interfaceName)){ Class<?> forName; try { forName = Class.forName(interfaceName); } catch (ClassNotFoundException e) { throw new IllegalArgumentException("interface class not found", e); } builder.addPropertyValue("interfaceName", forName); } } }
5、 创建Handler文件,扩展自NamespaceHandlerSupport,目的是将组件注册到Spring容器中,如StrategyBeanHandler
/** * 解析器注入 * @author shijun03 */ public class StrategyBeanHandler extends NamespaceHandlerSupport{ @Override public void init() { registerBeanDefinitionParser("strategy", new StrategyBeanDefinitionParser()); } }
6、编写Spring.handlers和Spring.schemas文件
路径:src/main/resources/META-INF/Spring.handlers
http\://www.shijun.com/schema/strategy=com.sjz.spring.schema.StrategyBeanHandler
路径:src/main/resources/META-INF/Spring.schemas
http\://www.shijun.com/schema/strategy.xsd=META-INF/strategy.xsd
7、创建测试配置文件
新建spring工程,引入前面创建的工程jar以及其他的jar
<dependency> <groupId>com.sjz</groupId> <artifactId>spring-schema</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> //…省略其他的jar
创建FristStrategy对象
public class FristStrategy { public String hello(){ return "hello schema"; } }
在src/main/resources下添加配置文件,如下spring-strategy.xml
<?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:pre="http://www.shijun.com/schema/strategy" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.shijun.com/schema/strategy http://www.shijun.com/schema/strategy.xsd"> <pre:strategy id="strategyFrist" interface="org.test.spring.schema.strategy.FristStrategy" url="tcp://strategy/frist" /> </beans>
通过Configuration注解引入spring-strategy.xml配置文件
@Configuration @ImportResource(locations="spring-strategy.xml") public class StrategyConfig { }
测试
@SpringBootApplication public class App { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(App.class, args); FristStrategy bean = context.getBean(FristStrategy.class); System.out.println("数据调用结果集========"+bean.hello()); } }
输出如下
数据调用结果集========hello schema
8、详细代码
spring自定义标签工程:https://github.com/ejshi/spring-schema.git 测试工程:https://github.com/ejshi/es/tree/master/test-spring-schema
相关推荐
spring的自定义标签代码工程样例,Chapter2工程
Spring 5.2.9 06 源码分析-spring自定义标签解析过程
Spring 5.2.9 06 源码分析-spring自定义标签解析过程
主要介绍了这一次搞懂Spring自定义标签以及注解解析原理说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
这一次搞懂Spring自定义标签以及注解解析原理说明.docx
扩展Spring, 自定义标签的实现, 包括 schema配置和自定义标签以及handler的加载过程
NULL 博文链接:https://wangxinchun.iteye.com/blog/2301263
用于自定义spring xml标签, 例如 <dubbo:service /> 标签
spring mvc freemarker 自定义标签demo spring mvc 通过自定义的标签在jsp页面调用,初学,记录下做个备忘
Spring框架是现在Java最流行的开源框架之一,需要实现一些自定义的标签,主要是方便使用我们框架的人能够快速、简单进行配置,有兴趣的可以了解一下。
实现spring自定义扩展标签的实现步骤
用springMVC+velocity时,当使用自定义标签,发现自定义标签无法注入spring的其它服务类,归根源码发现,velocity的源码中有一段,有一段代码永远获取的是新实例。所以,把这个地方的源码改了,所以使用这个的时候,...
Spring5.0自定义配置文件便签[Maven]工程可运行【spring-customize-tag】
主要给大家介绍了关于Spring源码解密之自定义标签与解析的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参借鉴,下面随着小编来一起学习学习吧。
spring自定义标签解析过程
jsp自定义标签,比如说if,foreach,out,format
spring 自定义标签技术 xml扩展例子,根据网上的文档整理的
结合spring,自定义xml标签,并利用factorybean来代理获取到的标签内容
spring自定义标签解析断点环境