Spring拡張性Schemaベースのカスタム構成サポート



From:http://blog.csdn.net/cutesource/article/details/5864562
多くの場合、システムに構成可能なサポートを提供する必要があります.簡単な方法でSpringの標準Beanに直接基づいて構成することができますが、構成が複雑な場合や、より豊富な制御が必要な場合は、非常に不器用に見えます.一般的な方法は、定義されたxmlファイルを元の生態的な方法で解析し、構成対象に変換します.この方法はもちろんすべての問題を解決することができますが、実現は煩雑です.特に、構成が非常に複雑な場合、解析作業は考慮しなければならない負担です.Springは拡張可能なSchemaのサポートを提供しています.これは良い折衷案で、カスタム構成を完了するには一般的に次の手順が必要です.
設計構成プロパティとJavaBean
XSDファイルの作成
NamespaceHandlerとBeanDefinitionParserを作成して解析を完了
spring.handlersとspring.schemasを作成してすべての部品を直列に接続します
Beanファイルへの適用
次は小さな例を結びつけて以上の過程を実戦する.
1)設計構成属性とJavaBean
まず、もちろん構成項目を設計し、JavaBeanによってモデリングする必要があります.この例ではPeopleエンティティを構成し、属性nameとageを構成する必要があります(idはデフォルトで必要です).
public class People {  
    private String id;  
    private String name;  
    private Integer age;  
}  
2)XSDファイルの作成
前回設計したコンフィギュレーション項目のためにXSDファイルを作成し、XSDはschemaの定義ファイルであり、コンフィギュレーションの入力と解析出力はいずれもXSDを契約としており、この例ではXSDは以下のようになっている.
   
   
   
   
  1. xml version="1.0" encoding="UTF-8"?>   
  2.  
  3. <xsd:schema    
  4.  
  5.     xmlns="http://blog.csdn.net/cutesource/schema/people"   
  6.  
  7.     xmlns:xsd="http://www.w3.org/2001/XMLSchema"    
  8.  
  9.     xmlns:beans="http://www.springframework.org/schema/beans"   
  10.  
  11.     targetNamespace="http://blog.csdn.net/cutesource/schema/people"   
  12.  
  13.     elementFormDefault="qualified"    
  14.  
  15.     attributeFormDefault="unqualified">   
  16.  
  17.     <xsd:import namespace="http://www.springframework.org/schema/beans" />   
  18.  
  19.     <xsd:element name="people">   
  20.  
  21.         <xsd:complexType>   
  22.  
  23.             <xsd:complexContent>   
  24.  
  25.                 <xsd:extension base="beans:identifiedType">   
  26.  
  27.                     <xsd:attribute name="name" type="xsd:string" />   
  28.  
  29.                     <xsd:attribute name="age" type="xsd:int" />   
  30.  
  31.                 xsd:extension>   
  32.  
  33.             xsd:complexContent>   
  34.  
  35.         xsd:complexType>   
  36.  
  37.     xsd:element>   
  38.  
  39. xsd:schema>   

xsd:schemaの各プロパティの具体的な意味については、あまり説明しないでください.http://www.w3school.com.cn/schema/schema_schema.asp
コンフィギュレーション・アイテム・ノードの名前に対応するため、アプリケーションではpeopleをノード名としてこのコンフィギュレーションを参照します.
とコンフィギュレーションアイテムpeopleに対応する2つのプロパティ名を持つため、アプリケーションではstringとintタイプのnameとageの2つのプロパティをコンフィギュレーションできます.
完成したらxsdをclasspathの下に置く必要があり、一般的にMETA-INFディレクトリの下に置く(この例ではこのディレクトリの下に置く)
【注意】xsdエラー:src-resolve:Cannot resolve the name'beans:identifiedType'to a(n)'type definition'component.
 スキップしてください.これは使用に影響しません.ideの検査に問題があります.
3)NamespaceHandlerとBeanDefinitionParserを作成して解析を完了する
NamespaceHandlerとBeanDefinitionParserの2つの概念を使用して解析を完了する必要があります.具体的には、NamespaceHandlerはschemaとノード名に基づいてBeanDefinitionParserを見つけ、BeanDefinitionParserによって具体的な解析を完了します.そのため、NamespaceHandlerとBeanDefinitionParserの実装クラスを別々に完了する必要があります.Springはデフォルトの実装クラスNamespaceHandlerSupportとAbstractSingleBeanDefinitionParserを提供しています.簡単な方法は、この2つのクラスを継承することです.この例では、
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;  
public class MyNamespaceHandler extends NamespaceHandlerSupport {  
    public void init() {  
        registerBeanDefinitionParser("people", new PeopleBeanDefinitionParser());  
    }  
}  
ここでregisterBeanDefinitionParser(「people」、new PeopleBeanDefinitionParser()は、ノード名と解析クラスを関連付けるために使用され、構成でpeople構成項目を参照すると、PeopleBeanDefinitionParserで構成を解析します.PeopleBeanDefinitionParserは、この例の解析クラスです.
   
   
   
   
  1. import org.springframework.beans.factory.support.BeanDefinitionBuilder;   
  2.  
  3. import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;   
  4.  
  5. import org.springframework.util.StringUtils;   
  6.  
  7. import org.w3c.dom.Element;   
  8.  
  9. public class PeopleBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {   
  10.  
  11.     protected Class getBeanClass(Element element) {   
  12.  
  13.         return People.class;   
  14.  
  15.     }   
  16.  
  17.     protected void doParse(Element element, BeanDefinitionBuilder bean) {   
  18.  
  19.         String name = element.getAttribute("name");   
  20.  
  21.         String age = element.getAttribute("age");   
  22.  
  23.         String id = element.getAttribute("id");   
  24.  
  25.         if (StringUtils.hasText(id)) {   
  26.  
  27.             bean.addPropertyValue("id", id);   
  28.  
  29.         }   
  30.  
  31.         if (StringUtils.hasText(name)) {   
  32.  
  33.             bean.addPropertyValue("name", name);   
  34.  
  35.         }   
  36.  
  37.         if (StringUtils.hasText(age)) {   
  38.  
  39.             bean.addPropertyValue("age", Integer.valueOf(age));   
  40.  
  41.         }   
  42.  
  43.     }   
  44.  
  45. }   

ここでelement.getAttributeは構成で属性値を取得し、bean.addPropertyValueは属性値をbeanに配置します.
4)spring.handlersとspring.schemasを作成してすべての部品を直列に接続する
上のいくつかのステップを歩いていくと、開発されたhandlerとxsdはまだアプリケーションに感知されていないことがわかります.このように置くと、前の仕事をシステムに組み込むことができません.springはspring.handlersとspring.schemasの2つのプロファイルを提供してこの仕事を完成させます.この2つのファイルは私たちが自分で作成してMETA-INFフォルダに入れる必要があります.この2つのファイルのアドレスMETA-INF/spring.handlersとMETA-INF/spring.schemasである必要があります.springはデフォルトでロードされます.この例ではspring.handlersは次のようになります.
http\://blog.csdn.net/cutesource/schema/people=study.schemaExt.MyNamespaceHandler
如果使用到名为"http://blog.csdn.net/cutesource/schema/people「のschema参照の場合、study.schemaExt.MyNamespaceHandlerで解析が完了します.
Spring.schemasは次のとおりです.
http\://blog.csdn.net/cutesource/schema/people.xsd=META-INF/people.xsd
以上xsdファイルの読み込み
5)Beanファイルへの適用
ここまでの簡単なカスタム構成では、特定のアプリケーションで使用できます.使用方法は簡単で、一般的なspring beanの構成と似ていますが、schemaをカスタマイズする必要があります.この例では、以下のように参照します.
 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:cutesource="http://blog.csdn.net/cutesource/schema/people" 
    xsi:schemaLocation=" 
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
http://blog.csdn.net/cutesource/schema/people http://blog.csdn.net/cutesource/schema/people.xsd">  
     
 
ここでxmlns:cutesource="http://blog.csdn.net/cutesource/schema/people「カスタムschemaを指定し、xsi:schemaLocationを使用してxsdファイルを指定します.具体的なカスタム構成の使用例です.
最後に、特定のプログラムで基本的なbeanロード方式を使用して、カスタム構成オブジェクトをロードできます.たとえば、次のようにします.
ApplicationContext ctx = new ClassPathXmlApplicationContext("application.xml");  
People p = (People)ctx.getBean("cutesource");  
System.out.println(p.getId());  
System.out.println(p.getName());  
System.out.println(p.getAge());  
出力:
cutesource
袁志俊
27