1. 程式人生 > >基於Spring可擴充套件Schema提供自定義配置支援

基於Spring可擴充套件Schema提供自定義配置支援

spring 可以基於schema 擴充套件,自定義 schema。參考文件自己搭了個應用試驗了一下:


首先看下自己寫的 myns.xsd

Xml程式碼  收藏程式碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <xsd:schema xmlns="http://www.yjhexy.com/schema/myns"  
  3.     xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:beans="http://www.springframework.org/schema/beans"  
  4.     targetNamespace
    ="http://www.yjhexy.com/schema/myns" elementFormDefault="qualified"  
  5.     attributeFormDefault="unqualified">  
  6.     <xsd:import namespace="http://www.springframework.org/schema/beans" />  
  7.     <xsd:element name="dateformat">  
  8.         <xsd:complexType>  
  9.             <xsd:complexContent
    >  
  10.                 <xsd:extension base="beans:identifiedType">  
  11.                     <xsd:attribute name="lenient" type="xsd:boolean" />  
  12.                     <xsd:attribute name="pattern" type="xsd:string" use="required" />  
  13.                 </xsd:extension>  
  14.             </
    xsd:complexContent>  
  15.         </xsd:complexType>  
  16.     </xsd:element>  
  17. </xsd:schema>  

然後看下我的applicationContxt.xml配置:

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:myns="http://www.yjhexy.com/schema/myns"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  6.     http://www.yjhexy.com/schema/myns http://www.yjhexy.com/schema/myns/myns.xsd  
  7.     ">  
  8.     <myns:dateformat id="dateFormat" pattern="yyyy-MM-dd HH:mm"  
  9.         lenient="true" />  
  10. </beans>  

很明顯實現了個自定義的bean ,這個bean有兩個屬性,一個是時間的格式,另外一個不知道啥東西。

然後在META-INF下面寫了兩個檔案,

spring.handlers:用來描述如何處理自定義的namespace

Xml程式碼  收藏程式碼
  1. http\://www.yjhexy.com/schema/myns=com.yajun.balance.spring.MyNamespaceHandler  

 spring.schemas:描述schema的位置

Xml程式碼  收藏程式碼
  1. http\://www.yjhexy.com/schema/myns/myns.xsd=com/yajun/balance/spring/myns.xsd  

然後是需要兩個類,一個處理namespace,一個處理beanDefinition

Java程式碼  收藏程式碼
  1. package com.yajun.balance.spring;  
  2. import org.springframework.beans.factory.xml.NamespaceHandlerSupport;  
  3. public class MyNamespaceHandler extends NamespaceHandlerSupport {  
  4.     public void init() {  
  5.         registerBeanDefinitionParser("dateformat"new SimpleDateFormatBeanDefinitionParser());  
  6.     }  
  7. }  
Java程式碼  收藏程式碼
  1. package com.yajun.balance.spring;  
  2. import java.text.SimpleDateFormat;  
  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. public class SimpleDateFormatBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {  
  8.     protected Class getBeanClass(Element element) {  
  9.         return SimpleDateFormat.class;  
  10.     }  
  11.     protected void doParse(Element element, BeanDefinitionBuilder bean) {  
  12.         // this will never be null since the schema explicitly requires that a value be supplied  
  13.         String pattern = element.getAttribute("pattern");  
  14.         bean.addConstructorArgValue(pattern);  
  15.         // this however is an optional property  
  16.         String lenient = element.getAttribute("lenient");  
  17.         if (StringUtils.hasText(lenient)) {  
  18.             bean.addPropertyValue("lenient", Boolean.valueOf(lenient));  
  19.         }  
  20.     }  
  21. }  

最後main輸出試試:

Java程式碼  收藏程式碼
  1. package com.yajun.balance.spring;  
  2. import java.text.SimpleDateFormat;  
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5. public class MainTest {  
  6.     public static void main(String[] args) {  
  7.         ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");  
  8.         SimpleDateFormat f = (SimpleDateFormat) ctx.getBean("dateFormat");  
  9.         System.out.println(f);  
  10.     }  
  11. }