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

 

  1. public class User {
  2. private String userName;
  3. private String email;
  4. public String getUserName() {
  5. return userName;
  6. }
  7. public void setUserName(String userName) {
  8. this.userName = userName;
  9. }
  10. public String getEmail() {
  11. return email;
  12. }
  13. public void setEmail(String email) {
  14. this.email = email;
  15. }
  16. }

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

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

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

  1. package vip.spring.demo.customTag;
  2. import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
  3. public class MyNamespaceHandler extends NamespaceHandlerSupport {
  4. public void init() {
  5. registerBeanDefinitionParser("user", new UserBeanDefinitionParser());
  6. }
  7. }

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

  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

http\://www.lexueba.com/schema/user=vip.spring.demo.customTag.MyNamespaceHandler

spring.schemas

http\://www.lexueba.com/schema/user.xsd=META-INF/spring-test.xsd

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


test.xml 

  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

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


整个项目目录如下:


项目 pom.xml

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