spring 自定义schema

扩展schema,定义自己的bean属性。。不错!

主要:

1,定义META-INF下.xsd文件,这里是people.xsd;定义spring.handlers;定义spring.schemas

2,定义namaspace解析类,这里是StudentNamespaceHandler

3,定义beanDefinition,这里是StudentBeanDefinitionParser

4,当然还有相关的javabean定义,这里是Student.java

详细代码:

people.xsd

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <xsd:schema xmlns="http://www.luyee.com/bat/schema/people"  
  3.     xmlns:xsd="http://www.w3.org/2001/XMLSchema"   
  4.     xmlns:beans="http://www.springframework.org/schema/beans"  
  5.     targetNamespace="http://www.luyee.com/bat/schema/people"  
  6.     elementFormDefault="qualified"   
  7.     attributeFormDefault="unqualified">  
  8.     <xsd:import namespace="http://www.springframework.org/schema/beans" />  
  9.       
  10.     <xsd:element name="student">  
  11.      <xsd:complexType>    
  12.             <xsd:complexContent>    
  13.                 <xsd:extension base="beans:identifiedType">   
  14.                    
  15.                     <xsd:attribute name="name"  
  16.                         type="xsd:string">  
  17.                         <xsd:annotation>  
  18.                             <xsd:documentation>  
  19.                                 姓名   
  20.                             </xsd:documentation>  
  21.                         </xsd:annotation>  
  22.                     </xsd:attribute>   
  23.                        
  24.                     <xsd:attribute name="age"  
  25.                         type="xsd:string">  
  26.                         <xsd:annotation>  
  27.                             <xsd:documentation>  
  28.                                 年龄  
  29.                             </xsd:documentation>  
  30.                         </xsd:annotation>  
  31.                     </xsd:attribute>    
  32.                      
  33.                 </xsd:extension>    
  34.             </xsd:complexContent>    
  35.         </xsd:complexType>    
  36.     </xsd:element>    
  37.    </xsd:schema>  

spring.handlers;

[html] view plain copy
  1. http\://www.luyee.com/bat/schema/people=com.luyee.bat.spring.StudentNamespaceHandler  

spring.schemas

[html] view plain copy
  1. http\://www.luyee.com/bat/schema/people.xsd=META-INF/people.xsd  

StudentNamespaceHandler和StudentBeanDefinitionParser

[java] view plain copy
  1. package com.luyee.bat.spring;  
  2.   
  3. import java.text.SimpleDateFormat;  
  4.   
  5. import org.springframework.beans.factory.support.BeanDefinitionBuilder;  
  6. import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;  
  7. import org.springframework.beans.factory.xml.NamespaceHandlerSupport;  
  8. import org.springframework.util.StringUtils;  
  9. import org.w3c.dom.Element;  
  10.   
  11. public class StudentNamespaceHandler  extends NamespaceHandlerSupport {  
  12.   
  13.     public void init() {  
  14.         registerBeanDefinitionParser("student"new StudentBeanDefinitionParser());    
  15.     }  
  16.       
  17.     class StudentBeanDefinitionParser extends AbstractSingleBeanDefinitionParser{  
  18.          protected Class getBeanClass(Element element) {    
  19.                 return Student.class;    
  20.             }    
  21.             
  22.             protected void doParse(Element element, BeanDefinitionBuilder bean) {    
  23.                 String name = element.getAttribute("name");    
  24.                 bean.addPropertyValue("name", name);    
  25.   
  26.                 String age = element.getAttribute("age");    
  27.                 if (StringUtils.hasText(age)) {    
  28.                     bean.addPropertyValue("age", Integer.valueOf(age));    
  29.                 }    
  30.             }    
  31.     }  
  32. }  

JavaBean:Student

[java] view plain copy
  1. package com.luyee.bat.spring;  
  2.   
  3. public class Student {  
  4.       
  5.     private String name;  
  6.       
  7.     private int age;  
  8.   
  9.     public String getName() {  
  10.         return name;  
  11.     }  
  12.   
  13.     public void setName(String name) {  
  14.         this.name = name;  
  15.     }  
  16.   
  17.     public int getAge() {  
  18.         return age;  
  19.     }  
  20.   
  21.     public void setAge(int age) {  
  22.         this.age = age;  
  23.     }  
  24.       
  25.       
  26.   
  27. }  


测试:applicationContex.xml(people:student就好比bean)

[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:people="http://www.luyee.com/bat/schema/people"    
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd    
  6.     http://www.luyee.com/bat/schema/people http://www.luyee.com/bat/schema/people.xsd" >    
  7.     <people:student id="student1" name="student1"    
  8.         age="18" />    
  9.           
  10.     <people:student id="student2" name="student2"    
  11.         age="20" />    
  12.     
  13. </beans>   

StudentXsdTest.java

[java] view plain copy
  1. package com.luyee.bat.spring;  
  2.   
  3.     
  4. import org.springframework.context.ApplicationContext;    
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;    
  6.     
  7. public class StudentXsdTest {    
  8.     
  9.     public static void main(String[] args) {    
  10.         ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");    
  11.         Student student1 = (Student) ctx.getBean("student1");    
  12.         Student student2 = (Student) ctx.getBean("student2");    
  13.         System.out.println("name: " +student1.getName()+" age :" + student1.getAge());    
  14.         System.out.println("name: " +student2.getName()+" age :" + student2.getAge());    
  15.     }    
  16. }    

输出:

[plain] view plain copy
  1. 八月 13, 2013 8:50:50 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh  
  2. INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@46f7ba12: startup date [Tue Aug 13 20:50:50 CST 2013]; root of context hierarchy  
  3. 八月 13, 2013 8:50:50 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions  
  4. INFO: Loading XML bean definitions from class path resource [applicationContext.xml]  
  5. 八月 13, 2013 8:50:52 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons  
  6. INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@25e9a396: defining beans [student1,student2]; root of factory hierarchy  
  7. name: student1 age :18  
  8. name: student2 age :20  

最后贴个文件结构

http://blog.csdn.net/luyee2010/article/details/9954941

猜你喜欢

转载自m635674608.iteye.com/blog/2324384