前言

之前咱们聊过自定义的SPI如何与spring进行整合,明天咱们就来聊下如何通过自定义标签将spi对象注入到spring容器中

实现套路

1、自定义xsd

示例

<?xml version="1.0" encoding="UTF-8" standalone="no"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"            xmlns:beans="http://www.springframework.org/schema/beans"            xmlns:tool="http://www.springframework.org/schema/tool"            xmlns="http://lybgeek.github.com/schema/spi"            targetNamespace="http://lybgeek.github.com/schema/spi">    <xsd:import namespace="http://www.w3.org/XML/1998/namespace"/>    <xsd:import namespace="http://www.springframework.org/schema/beans"                schemaLocation="http://www.springframework.org/schema/beans/spring-beans.xsd"/>    <xsd:import namespace="http://www.springframework.org/schema/tool"/>    <xsd:annotation>        <xsd:documentation>            <![CDATA[ Namespace support for spi services ]]></xsd:documentation>    </xsd:annotation>    <xsd:complexType name="scanType">        <xsd:attribute name="id" type="xsd:ID">            <xsd:annotation>                <xsd:documentation><![CDATA[ The unique identifier for a bean. ]]></xsd:documentation>            </xsd:annotation>        </xsd:attribute>        <xsd:attribute name="basePackages" type="xsd:string" use="required">            <xsd:annotation>                <xsd:documentation><![CDATA[ Specify the spi package name to scan, multiple scan packages are separated by commas ]]></xsd:documentation>            </xsd:annotation>        </xsd:attribute>    </xsd:complexType>    <xsd:complexType name="interceptorType">        <xsd:attribute name="id" type="xsd:ID">            <xsd:annotation>                <xsd:documentation><![CDATA[ The unique identifier for a bean. ]]></xsd:documentation>            </xsd:annotation>        </xsd:attribute>        <xsd:attribute name="class" type="xsd:string" use="required">            <xsd:annotation>                <xsd:documentation><![CDATA[ Interceptor class name]]></xsd:documentation>            </xsd:annotation>        </xsd:attribute>    </xsd:complexType>    <xsd:complexType name="interceptorChainType">        <xsd:choice>            <xsd:element ref="interceptor" minOccurs="1" maxOccurs="unbounded"/>        </xsd:choice>    </xsd:complexType>    <xsd:element name="scan" type="scanType">        <xsd:annotation>            <xsd:documentation><![CDATA[ The scan config ]]></xsd:documentation>        </xsd:annotation>    </xsd:element>    <xsd:element name="interceptor" type="interceptorType">        <xsd:annotation>            <xsd:documentation><![CDATA[ The interceptor config ]]></xsd:documentation>        </xsd:annotation>    </xsd:element>    <xsd:element name="interceptorChain" type="interceptorChainType">        <xsd:annotation>            <xsd:documentation><![CDATA[ The interceptorChainType config ]]></xsd:documentation>        </xsd:annotation>    </xsd:element></xsd:schema>

ps: 如果对xsd不相熟的敌人,能够参考如下链接

https://www.w3school.com.cn/schema/index.asp

2、自定义解析BeanDefinitionParser解析器

示例:

public class AnnotationBeanDefinitionParser implements BeanDefinitionParser {    private final Class<?> beanClass;    public AnnotationBeanDefinitionParser(Class<?> beanClass) {        this.beanClass = beanClass;    }    @Override    public BeanDefinition parse(Element element, ParserContext parserContext) {        String packageToScan = element.getAttribute("basePackages");        String[] packagesToScan = trimArrayElements(commaDelimitedListToStringArray(packageToScan));        RootBeanDefinition beanDefinition = new RootBeanDefinition();        beanDefinition.setBeanClass(beanClass);        beanDefinition.setLazyInit(false);        beanDefinition.getConstructorArgumentValues().addIndexedArgumentValue(0,packagesToScan);        String beanName = BeanUtils.generateBeanName(element,"id",parserContext,beanClass.getName());        parserContext.getRegistry().registerBeanDefinition(beanName,beanDefinition);        return beanDefinition;    }}
3、定义NamespaceHandler实现类解决自定义标签的处理器

示例:

public class SpiNamespaceHandler extends NamespaceHandlerSupport {    @Override    public void init() {        registerBeanDefinitionParser("scan", new AnnotationBeanDefinitionParser(SpiAnnotationPostProcessor.class));    }}
4、将写入处理器、标签的地位写入spring.handlers、spring.schemas中

示例:

spring.handlers

http\://lybgeek.github.com/schema/spi=com.github.lybgeek.spring.schema.SpiNamespaceHandler

spring.schemas

http\://lybgeek.github.com/schema/spi/spi.xsd=META-INF/spi/spi.xsd

注: spring.handlers、spring.schemas需搁置在resource/META-INF目录底下

示例演示

1、配置xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns:spi="http://lybgeek.github.com/schema/spi"       xmlns="http://www.springframework.org/schema/beans"       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd       http://lybgeek.github.com/schema/spi http://lybgeek.github.com/schema/spi/spi.xsd">    <spi:scan basePackages="com.github.lybgeek"></spi:scan>
2、在启动类上导入xml
@SpringBootApplication@ImportResource(locations = "classpath:/spi.xml")public class SpiTestXmlApplication {    public static void main(String[] args) throws Exception{        SpringApplication.run(SpiTestXmlApplication.class);    }}
3、验证SPI是否注入spring容器
 @Override    public void run(ApplicationArguments args) throws Exception {        applicationContext.getBeansOfType(SpringSqlDialect.class)                .forEach((beanName,bean) -> System.out.println(beanName + "-->" + bean));    }

控制台输出如下

springMysqlDialect-->com.github.lybgeek.dialect.mysql.SpringMysqlDialect@73041b7dmysql-hello-->com.github.lybgeek.dialect.mysql.SpringMysqlDialect@574059d5springOracleDialect-->com.github.lybgeek.dialect.oracle.SpringOracleDialect@4a50d04a

阐明曾经导入到spring容器中

总结

自从spring3+开始引入注解驱动后,在新我的项目基本上很少会应用xml,但如果是一些老旧的我的项目,大家如果想实现自定义标签注入到spring,就能够应用本文的形式。

套路就是如下

  •   1、自定义xsd
  •   2、自定义解析BeanDefinitionParser解析器
  •   3、定义NamespaceHandler实现类解决自定义标签的处理器
  •   4、将写入处理器、标签的地位写入spring.handlers、spring.schemas中

本文的实现也是绝对简略,如果想深刻应用,举荐看看dubbo自定义spring标签

demo链接

https://github.com/lyb-geek/springboot-learning/tree/master/springboot-spi-enhance/springboot-spi-framework-spring