Spring自定义标签

2018-01-30  本文已影响0人  一滴水的坚持

在读elastic-job过程中,发现使用了spring的自定义标签,当时没有细看实现原理,只关心了elastic-job的调度原理,现在开始研究dubbo源代码,dubbo服务启动第一步就是需要将服务注册到注册中心,这里读取了大量的spring自定义标签,比如<dubbo:service>等等,正好先看一下Spring自定义标签的扩展功能。

Spring自定义标签总共可以分为以下几个步骤

按照如上步骤,demo如下:

public class User {
    private String id;
    private String userName;
    private String email;
   //省略get/set方法。
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns="http://jettyrun.github.com/schema/user" targetNamespace="http://jettyrun.github.com/schema/user"
            elementFormDefault="qualified">
    <xsd:element name="user">
        <xsd:complexType>
            <xsd:attribute name="id" type="xsd:string" />
            <xsd:attribute name="userName" type="xsd:string" />
            <xsd:attribute name="email" type="xsd:string" />
        </xsd:complexType>
    </xsd:element>
</xsd:schema>
package com.github.jettyrun.springtag.parser;

import com.github.jettyrun.springtag.bean.User;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;

/**
 * Created by jetty on 18/1/30.
 */
public class UserBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
    @Override
    protected Class<?> getBeanClass(Element element) {
        return User.class;
    }
    

    @Override
    protected void doParse(Element element, BeanDefinitionBuilder builder) {
        String userName=element.getAttribute("userName");
        String email=element.getAttribute("email");
        if(StringUtils.hasText(userName)){
            builder.addPropertyValue("userName", userName);
        }
        if(StringUtils.hasText(email)){
            builder.addPropertyValue("email", email);
        }
    }
}

package com.github.jettyrun.springtag.namespace;


import com.github.jettyrun.springtag.parser.UserBeanDefinitionParser;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;

/**
 * Created by jetty on 18/1/30.
 */
public class UserNamespaceHandler extends NamespaceHandlerSupport{
    public void init() {

        registerBeanDefinitionParser("user",new UserBeanDefinitionParser());
    }
}
http\://jettyrun.github.com/schema/user.xsd=META-INF/spring-user.xsd
http\://jettyrun.github.com/schema/user=com.github.jettyrun.springtag.namespace.UserNamespaceHandler

定义完成,最后,写一个main方法运行一下:

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:tx="http://www.springframework.org/schema/tx"
      <!-- 定义user标签前缀为springtag -->
       xmlns:springtag="http://jettyrun.github.com/schema/user"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://jettyrun.github.com/schema/user http://jettyrun.github.com/schema/user.xsd">
  <!--  声明一个user对象-->
    <springtag:user id="testBean" userName="name" email="wjs@163.com"/>
</beans>
package com.github.jettyrun.springtag;

import com.github.jettyrun.springtag.bean.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by jetty on 18/1/30.
 */
public class Main {

    public static void main(String[] args) {
        ApplicationContext beans=new ClassPathXmlApplicationContext("classpath:application-usertag.xml");
        User user=(User)beans.getBean("testBean");
        System.out.println("username:"+user.getUserName()+":"+"email:"+user.getEmail());
    }
}
username:name:email:wjs@163.com

可以看到,user就是自己定义的一个自定义标签。
代码来自博客:http://blog.csdn.net/shinebar/article/details/75675615
代码地址:https://gitee.com/shxz130/SpringTag/
fyi

上一篇 下一篇

猜你喜欢

热点阅读