Spring自定义标签

一、创建一个普通的Person类

/**
 * 自定义标签实体类
 */
public class Person {
    private String id;
    private String personName;
    private String email;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getPersonName() {
        return personName;
    }
    public void setPersonName(String personName) {
        this.personName = personName;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
}

二、创建PersonBeanDefinitionParser类继承AbstractSingleBeanDefinitionParser重写doParse和getBeanClass方法

/**
 * 自定义标签
 */
public class PersonBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {

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

    @Override
    protected Class<?> getBeanClass(Element element) {
        return Person.class;
    }
}

三、创建MyPersonNamespaceHandler类继承AbstractSingleBeanDefinitionParser

/**
 * 注册自定义标签到Spring框架中,命名为:person1
 */
public class MyPersonNamespaceHandler extends NamespaceHandlerSupport {
    @Override
    public void init() {
        registerBeanDefinitionParser("person1",new PersonBeanDefinitionParser());
    }
}

四、创建spring-person.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns="http://www.kang.com/schema/person" targetNamespace="http://www.kang.com/schema/person"
            elementFormDefault="qualified">
    <xsd:element name="person1">
        <xsd:complexType>
            <xsd:attribute name="id" type="xsd:string" />
            <xsd:attribute name="personName" type="xsd:string" />
            <xsd:attribute name="email" type="xsd:string" />
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

五、在classpath下创建一文件夹META-INF,然后创建两个文件spring.handlers和spring.schemas,(注意:这两个文件名不能改的)其中:为什么要创建MEAT-INF文件夹,spring是默认的向这个文件夹里面寻找这两个文件,如果想修改,就要修改spring源码来实现。

spring.handlers:

http\://www.kang.com/schema/person=com.example.cosumertag.MyPersonNamespaceHandler

spring.schemas:

http\://www.tan.com/schema/person.xsd=META-INF/spring-person.xsd


六、创建application-customtag.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: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"
       xmlns:myTag="http://www.kang.com/schema/person"
       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://www.kang.com/schema/person http://www.tan.com/schema/person.xsd">

    <!--使用自定义标签:person1-->
    <myTag:person1 id="customtag" personName="tankang" email="[email protected]"/>
</beans>

七、创建Test类进行测试

/**
 * 测试类
 */
public class Test {
    public static void main(String[] args) {
        ApplicationContext beans = new ClassPathXmlApplicationContext("application-customtag.xml");
        Person person = (Person) beans.getBean("customtag");
        System.out.println("personName:"+person.getPersonName()+":"+"email:"+person.getEmail());
    }
}

输出: personName:tankang:email:[email protected]

猜你喜欢

转载自blog.csdn.net/qq_37211608/article/details/80118300