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は以下のようになっている.
- xml version="1.0" encoding="UTF-8"?>
-
- <xsd:schema
-
- xmlns="http://blog.csdn.net/cutesource/schema/people"
-
- xmlns:xsd="http://www.w3.org/2001/XMLSchema"
-
- xmlns:beans="http://www.springframework.org/schema/beans"
-
- targetNamespace="http://blog.csdn.net/cutesource/schema/people"
-
- elementFormDefault="qualified"
-
- attributeFormDefault="unqualified">
-
- <xsd:import namespace="http://www.springframework.org/schema/beans" />
-
- <xsd:element name="people">
-
- <xsd:complexType>
-
- <xsd:complexContent>
-
- <xsd:extension base="beans:identifiedType">
-
- <xsd:attribute name="name" type="xsd:string" />
-
- <xsd:attribute name="age" type="xsd:int" />
-
- xsd:extension>
-
- xsd:complexContent>
-
- xsd:complexType>
-
- xsd:element>
-
- 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は、この例の解析クラスです.
- import org.springframework.beans.factory.support.BeanDefinitionBuilder;
-
- import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
-
- import org.springframework.util.StringUtils;
-
- import org.w3c.dom.Element;
-
- public class PeopleBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
-
- protected Class getBeanClass(Element element) {
-
- return People.class;
-
- }
-
- protected void doParse(Element element, BeanDefinitionBuilder bean) {
-
- String name = element.getAttribute("name");
-
- String age = element.getAttribute("age");
-
- String id = element.getAttribute("id");
-
- if (StringUtils.hasText(id)) {
-
- bean.addPropertyValue("id", id);
-
- }
-
- if (StringUtils.hasText(name)) {
-
- bean.addPropertyValue("name", name);
-
- }
-
- if (StringUtils.hasText(age)) {
-
- bean.addPropertyValue("age", Integer.valueOf(age));
-
- }
-
- }
-
- }
ここで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