spring自定义标签使用

原理实现请看:spring自定义标签原理


一、首先建立一个标签对应的实体类User

 

[java]  view plain  copy
  1. public class User {  
  2.     private String userName;  
  3.     private String email;  
  4.   
  5.     public String getUserName() {  
  6.         return userName;  
  7.     }  
  8.   
  9.     public void setUserName(String userName) {  
  10.         this.userName = userName;  
  11.     }  
  12.   
  13.     public String getEmail() {  
  14.         return email;  
  15.     }  
  16.   
  17.     public void setEmail(String email) {  
  18.         this.email = email;  
  19.     }  
  20.   
  21. }  

二、建立解析自定义标签的类 UserBeanDefinitionParser

[java]  view plain  copy
  1. package vip.spring.demo.customTag;  
  2.   
  3. import org.springframework.beans.factory.support.BeanDefinitionBuilder;  
  4. import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;  
  5. import org.springframework.util.StringUtils;  
  6. import org.w3c.dom.Element;  
  7.   
  8. public class UserBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {  
  9.   
  10.     protected Class getBeanClass(Element element) {  
  11.         return User.class;  
  12.     }  
  13.   
  14.     protected void doParse(Element element, BeanDefinitionBuilder bean) {  
  15.         String userName = element.getAttribute("userName");  
  16.         String email = element.getAttribute("email");  
  17.   
  18.         if (StringUtils.hasText(userName)) {  
  19.             bean.addPropertyValue("userName", userName);  
  20.         }  
  21.         if (StringUtils.hasText(email)) {  
  22.             bean.addPropertyValue("email", email);  
  23.         }  
  24.     }  
  25. }  

三、建立处理的类 NamespaceHandlerSupport,这个类主要用来调用解释类

[java]  view plain  copy
  1. package vip.spring.demo.customTag;  
  2.   
  3. import org.springframework.beans.factory.xml.NamespaceHandlerSupport;  
  4.   
  5. public class MyNamespaceHandler extends NamespaceHandlerSupport {  
  6.   
  7.     public void init() {  
  8.         registerBeanDefinitionParser("user"new UserBeanDefinitionParser());  
  9.     }  
  10.   
  11. }  

四、建立xsd 文件,用来校验我们的自定义标签的有效性

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.lexueba.com/schema/user"  
  3.     xmlns:tns="http://www.lexueba.com/schema/user" elementFormDefault="qualified">  
  4.     <element name="user">  
  5.         <complexType>  
  6.             <attribute name="id" type="string" />  
  7.             <attribute name="userName" type="string" />  
  8.             <attribute name="email" type="string" />  
  9.         </complexType>  
  10.     </element>  
  11. </schema>  

五、spring读取我们自定义的schema和handlers默认是根据spring.handlers和spring.schemas来读取的,这两个文件默认要放在META-INF下

具体配置:

spring.handlers

[html]  view plain  copy
  1. http\://www.lexueba.com/schema/user=vip.spring.demo.customTag.MyNamespaceHandler  

spring.schemas

[html]  view plain  copy
  1. http\://www.lexueba.com/schema/user.xsd=META-INF/spring-test.xsd  

六、配置完毕后,我们就可以使用自定义标签了,建立test.xml.建立的时候,如果eclipse没有提示,可以在Preferencers -> XML Catalog 下添加我们刚刚的xsd


test.xml 

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.      xmlns:myname="http://www.lexueba.com/schema/user"  
  5.      xsi:schemaLocation="  
  6.      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  7.      http://www.lexueba.com/schema/user http://www.lexueba.com/schema/user.xsd">  
  8.    <myname:user id="testBean" userName="cong" email="mail.163.com"/>  
  9. </beans>  

最后,编写test的文件  SpringCustomTest.java

[java]  view plain  copy
  1. package vip.spring.demo.customTag;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. public class SpringCustomTest {  
  7.   
  8.     public static void main(String[] args) {  
  9.         ApplicationContext ac = new ClassPathXmlApplicationContext("test.xml");  
  10.         User user = (User) ac.getBean("testBean");  
  11.         System.out.println(user.getUserName());  
  12.     }  
  13.   
  14. }  


整个项目目录如下:


项目 pom.xml

[html]  view plain  copy
  1. <properties>  
  2.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  3.     </properties>  
  4.   
  5.     <dependencies>  
  6.         <dependency>  
  7.             <groupId>org.springframework</groupId>  
  8.             <artifactId>spring-core</artifactId>  
  9.             <version>3.2.3.RELEASE</version>  
  10.         </dependency>  
  11.         <dependency>  
  12.             <groupId>org.springframework</groupId>  
  13.             <artifactId>spring-test</artifactId>  
  14.             <version>3.2.0.RELEASE</version>  
  15.         </dependency>  
  16.     </dependencies>  
  17.   
  18.     <build>  
  19.         <plugins>  
  20.             <plugin>  
  21.                 <groupId>org.apache.maven.plugins</groupId>  
  22.                 <artifactId>maven-compiler-plugin</artifactId>  
  23.                 <version>3.2</version>  
  24.                 <configuration>  
  25.                     <source>1.7</source>  
  26.                     <target>1.7</target>  
  27.                     <encoding>UTF8</encoding>  
  28.                 </configuration>  
  29.             </plugin>  
  30.         </plugins>  
  31.     </build>  


转载链接:http://blog.csdn.net/sonycong/article/details/52386783

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值