欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

自定义标签使用

程序员文章站 2022-03-04 11:51:38
...

(1)定义普通bean类

public class User {    
    private String id;    
    private String userName;    
    private String email;    
    public String getId() {    
        return id;    
    }    
    public void setId(String id) {    
        this.id = id;    
    }    
    public String getUserName() {    
        return userName;    
    }    
    public void setUserName(String userName) {    
        this.userName = userName;    
    }    
    public String getEmail() {    
        return email;    
    }    
    public void setEmail(String email) {    
        this.email = email;    
    }    
}  

(2)定义一个AbstractSingleBeanDefinitionParser,实现这个类里面的一个doParser和getBeanClass方法

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);    
        }    
    }    
} 
(3)定义一个nameSpaceHandlerSupport,用于注册到Spring框架里面,在spring解析算定义标签的时候可以用我们自定义的处理方式
public class MyUserNamespaceHandler extends NamespaceHandlerSupport {    
    
    @Override    
    public void init() {    
        registerBeanDefinitionParser("user",new UserBeanDefinitionParser());    
    }    
}  

(4)创建一个xsd为后缀的文件spring-user.xsd

<?xml version="1.0" encoding="UTF-8"?>    
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"    
    xmlns="http://www.wjs.com/schema/user" targetNamespace="http://www.wjs.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>  

(5)在META-INF下面创建两个文件spring.handlers和spring.schemas,内容如下:

spring.handlers:

http\://www.wjs.com/schema/user=com.wjs.cosumertag.MyUserNamespaceHandler  
spring.schemas:
http\://www.wjs.com/schema/user.xsd=META-INF/spring-user.xsd  

(6)在配置文件中我们测试使用自定义的标签:user.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.wjs.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-5.0.xsd    
        http://www.wjs.com/schema/user http://www.wjs.com/schema/user.xsd">    
     
 <myTag:user id="22" userName="测试" email="[email protected]"/>    
</beans>  

(7)测试的java类Main.java内容如下:

public class CustomerTagTest {    
    public static void main(String[] args) {    
        ApplicationContext beans=new ClassPathXmlApplicationContext("classpath:user.xml");    
        User user=(User)beans.getBean("testBean");    
        System.out.println("username:"+user.getUserName()+":"+"email:"+user.getEmail());    
    }    
}   
上一篇:Spring IOC容器的初始化(二)

下一篇:  自定义标签解析源码分析