最新の安定バージョンについては、Spring Framework 6.2.12 を使用してください! |
XML スキーマ
付録のこのパートには、コアコンテナーに関連する XML スキーマがリストされています。
util スキーマ
名前が示すように、util タグは、コレクションの構成、定数の参照など、一般的なユーティリティ構成の課題を処理します。util スキーマでタグを使用するには、Spring XML 構成ファイルの先頭に次のプリアンブルが必要です(スニペットのテキストは正しいスキーマを参照するため、util 名前空間のタグを使用できます)。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
<!-- bean definitions here -->
</beans><util:constant/> を使用する
以下の Bean 定義を考慮してください。
<bean id="..." class="...">
<property name="isolation">
<bean id="java.sql.Connection.TRANSACTION_SERIALIZABLE"
class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean" />
</property>
</bean> 上記の構成では、Spring FactoryBean 実装(FieldRetrievingFactoryBean)を使用して、Bean の isolation プロパティの値を java.sql.Connection.TRANSACTION_SERIALIZABLE 定数の値に設定します。これはすべてうまくいきますが、冗長であり、(不必要に)Spring の内部接続機能をエンドユーザーに公開します。
次の XML スキーマベースのバージョンはより簡潔で、開発者の意図を明確に表し(「この定数値を挿入する」)、読みやすくなっています。
<bean id="..." class="...">
<property name="isolation">
<util:constant static-field="java.sql.Connection.TRANSACTION_SERIALIZABLE"/>
</property>
</bean>フィールド値からの Bean プロパティまたはコンストラクター引数の設定
FieldRetrievingFactoryBean (Javadoc) は、static または非静的フィールド値を取得する FactoryBean です。通常、public static final 定数を取得するために使用され、その後、別の Bean のプロパティ値またはコンストラクター引数を設定するために使用できます。
次の例は、staticField (Javadoc) プロパティを使用して、static フィールドがどのように公開されるかを示しています。
<bean id="myField"
class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
<property name="staticField" value="java.sql.Connection.TRANSACTION_SERIALIZABLE"/>
</bean> 次の例に示すように、static フィールドが Bean 名として指定されている便利な使用形態もあります。
<bean id="java.sql.Connection.TRANSACTION_SERIALIZABLE"
class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"/> これは、Bean の id が何であるかはもはや選択の余地がないことを意味します(したがって、それを参照する他の Bean もこの長い名前を使用しなければなりません)が、この形式は定義が非常に簡潔であり、次の例が示すように、Bean の参照に対して id を指定する必要がないため、内側の Bean として使用するのに非常に便利です。
<bean id="..." class="...">
<property name="isolation">
<bean id="java.sql.Connection.TRANSACTION_SERIALIZABLE"
class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean" />
</property>
</bean>FieldRetrievingFactoryBean (Javadoc) クラスの API ドキュメントに従って、別の Bean の非静的(インスタンス)フィールドにアクセスすることもできます。
Spring では、プロパティ値またはコンストラクター引数として列挙値を Bean に注入するのは簡単です。実際には、Spring 内部(または FieldRetrievingFactoryBean などのクラスについて)について何もする必要も、何も知る必要もありません。次の列挙例は、列挙値を簡単に挿入できることを示しています。
Java
Kotlin
package jakarta.persistence;
public enum PersistenceContextType {
TRANSACTION,
EXTENDED
}
package jakarta.persistence
enum class PersistenceContextType {
TRANSACTION,
EXTENDED
}
次に、型 PersistenceContextType の以下の setter と、対応する Bean 定義を検討します。
Java
Kotlin
package example;
public class Client {
private PersistenceContextType persistenceContextType;
public void setPersistenceContextType(PersistenceContextType type) {
this.persistenceContextType = type;
}
}
package example
class Client {
lateinit var persistenceContextType: PersistenceContextType
}
<bean class="example.Client">
<property name="persistenceContextType" value="TRANSACTION"/>
</bean><util:property-path/> を使用する
次の例を考えてみましょう。
<!-- target bean to be referenced by name -->
<bean id="testBean" class="org.springframework.beans.TestBean" scope="prototype">
<property name="age" value="10"/>
<property name="spouse">
<bean class="org.springframework.beans.TestBean">
<property name="age" value="11"/>
</bean>
</property>
</bean>
<!-- results in 10, which is the value of property 'age' of bean 'testBean' -->
<bean id="testBean.age" class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/> 前述の構成では、Spring FactoryBean 実装(PropertyPathFactoryBean)を使用して、testBean Bean の age プロパティに等しい値を持つ testBean.age と呼ばれる Bean(型 int)を作成します。
ここで、<util:property-path/> 要素を追加する次の例を検討してください。
<!-- target bean to be referenced by name -->
<bean id="testBean" class="org.springframework.beans.TestBean" scope="prototype">
<property name="age" value="10"/>
<property name="spouse">
<bean class="org.springframework.beans.TestBean">
<property name="age" value="11"/>
</bean>
</property>
</bean>
<!-- results in 10, which is the value of property 'age' of bean 'testBean' -->
<util:property-path id="name" path="testBean.age"/><property-path/> 要素の path 属性の値は、beanName.beanProperty の形式に従います。この場合、testBean という名前の Bean の age プロパティを取得します。その age プロパティの値は 10 です。
<util:property-path/> を使用して Bean プロパティまたはコンストラクター引数を設定する
PropertyPathFactoryBean は、指定されたターゲットオブジェクトのプロパティパスを評価する FactoryBean です。ターゲットオブジェクトは、直接または Bean 名で指定できます。その後、この値を別の Bean 定義でプロパティ値またはコンストラクター引数として使用できます。
次の例は、名前によって別の Bean に対して使用されるパスを示しています。
<!-- target bean to be referenced by name -->
<bean id="person" class="org.springframework.beans.TestBean" scope="prototype">
<property name="age" value="10"/>
<property name="spouse">
<bean class="org.springframework.beans.TestBean">
<property name="age" value="11"/>
</bean>
</property>
</bean>
<!-- results in 11, which is the value of property 'spouse.age' of bean 'person' -->
<bean id="theAge"
class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
<property name="targetBeanName" value="person"/>
<property name="propertyPath" value="spouse.age"/>
</bean>次の例では、パスが内部 Bean に対して評価されます。
<!-- results in 12, which is the value of property 'age' of the inner bean -->
<bean id="theAge"
class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
<property name="targetObject">
<bean class="org.springframework.beans.TestBean">
<property name="age" value="12"/>
</bean>
</property>
<property name="propertyPath" value="age"/>
</bean>ショートカットフォームもあります。Bean 名はプロパティパスです。次の例は、ショートカットフォームを示しています。
<!-- results in 10, which is the value of property 'age' of bean 'person' -->
<bean id="person.age"
class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/> この形式は、Bean の名前に選択肢がないことを意味します。それへの参照も、パスである同じ id を使用する必要があります。内部 Bean として使用する場合、次の例に示すように、それを参照する必要はまったくありません。
<bean id="..." class="...">
<property name="age">
<bean id="person.age"
class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/>
</property>
</bean>実際の定義で結果型を明確に設定できます。これはほとんどのユースケースでは必要ありませんが、役に立つ場合があります。この機能の詳細については、javadoc を参照してください。
<util:properties/> を使用する
次の例を考えてみましょう。
<!-- creates a java.util.Properties instance with values loaded from the supplied location -->
<bean id="jdbcConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location" value="classpath:com/foo/jdbc-production.properties"/>
</bean> 上記の構成では、Spring FactoryBean 実装(PropertiesFactoryBean)を使用して、指定された Resource ロケーションからロードされた値を使用して java.util.Properties インスタンスをインスタンス化します。
次の例では、util:properties 要素を使用して、より簡潔な表現を作成しています。
<!-- creates a java.util.Properties instance with values loaded from the supplied location -->
<util:properties id="jdbcConfiguration" location="classpath:com/foo/jdbc-production.properties"/><util:list/> を使用する
次の例を考えてみましょう。
<!-- creates a java.util.List instance with values loaded from the supplied 'sourceList' -->
<bean id="emails" class="org.springframework.beans.factory.config.ListFactoryBean">
<property name="sourceList">
<list>
<value>[email protected] (英語) </value>
<value>[email protected] (英語) </value>
<value>[email protected] (英語) </value>
<value>[email protected] (英語) </value>
</list>
</property>
</bean> 上記の構成では、Spring FactoryBean 実装(ListFactoryBean)を使用して java.util.List インスタンスを作成し、指定された sourceList から取得した値で初期化します。
次の例では、<util:list/> 要素を使用して、より簡潔な表現を作成しています。
<!-- creates a java.util.List instance with the supplied values -->
<util:list id="emails">
<value>[email protected] (英語) </value>
<value>[email protected] (英語) </value>
<value>[email protected] (英語) </value>
<value>[email protected] (英語) </value>
</util:list> また、<util:list/> エレメントの list-class 属性を使用して、インスタンス化および移植される List の正確な型を明示的に制御できます。例: java.util.LinkedList をインスタンス化する必要がある場合、次の構成を使用できます。
<util:list id="emails" list-class="java.util.LinkedList">
<value>[email protected] (英語) </value>
<value>[email protected] (英語) </value>
<value>[email protected] (英語) </value>
<value>d'[email protected] (英語) </value>
</util:list>list-class 属性が指定されていない場合、コンテナーは List 実装を選択します。
<util:map/> を使用する
次の例を考えてみましょう。
<!-- creates a java.util.Map instance with values loaded from the supplied 'sourceMap' -->
<bean id="emails" class="org.springframework.beans.factory.config.MapFactoryBean">
<property name="sourceMap">
<map>
<entry key="pechorin" value="[email protected] (英語) "/>
<entry key="raskolnikov" value="[email protected] (英語) "/>
<entry key="stavrogin" value="[email protected] (英語) "/>
<entry key="porfiry" value="[email protected] (英語) "/>
</map>
</property>
</bean> 上記の構成では、Spring FactoryBean 実装(MapFactoryBean)を使用して、提供された 'sourceMap' から取得したキーと値のペアで初期化された java.util.Map インスタンスを作成します。
次の例では、<util:map/> 要素を使用して、より簡潔な表現を作成しています。
<!-- creates a java.util.Map instance with the supplied key-value pairs -->
<util:map id="emails">
<entry key="pechorin" value="[email protected] (英語) "/>
<entry key="raskolnikov" value="[email protected] (英語) "/>
<entry key="stavrogin" value="[email protected] (英語) "/>
<entry key="porfiry" value="[email protected] (英語) "/>
</util:map> また、<util:map/> エレメントの 'map-class' 属性を使用して、インスタンス化および移植される Map の正確な型を明示的に制御できます。例: java.util.TreeMap をインスタンス化する必要がある場合、次の構成を使用できます。
<util:map id="emails" map-class="java.util.TreeMap">
<entry key="pechorin" value="[email protected] (英語) "/>
<entry key="raskolnikov" value="[email protected] (英語) "/>
<entry key="stavrogin" value="[email protected] (英語) "/>
<entry key="porfiry" value="[email protected] (英語) "/>
</util:map>'map-class' 属性が指定されていない場合、コンテナーは Map 実装を選択します。
<util:set/> を使用する
次の例を考えてみましょう。
<!-- creates a java.util.Set instance with values loaded from the supplied 'sourceSet' -->
<bean id="emails" class="org.springframework.beans.factory.config.SetFactoryBean">
<property name="sourceSet">
<set>
<value>[email protected] (英語) </value>
<value>[email protected] (英語) </value>
<value>[email protected] (英語) </value>
<value>[email protected] (英語) </value>
</set>
</property>
</bean> 上記の構成では、Spring FactoryBean 実装(SetFactoryBean)を使用して、提供された sourceSet から取得した値で初期化された java.util.Set インスタンスを作成します。
次の例では、<util:set/> 要素を使用して、より簡潔な表現を作成しています。
<!-- creates a java.util.Set instance with the supplied values -->
<util:set id="emails">
<value>[email protected] (英語) </value>
<value>[email protected] (英語) </value>
<value>[email protected] (英語) </value>
<value>[email protected] (英語) </value>
</util:set> また、<util:set/> エレメントの set-class 属性を使用して、インスタンス化および移植される Set の正確な型を明示的に制御できます。例: java.util.TreeSet をインスタンス化する必要がある場合、次の構成を使用できます。
<util:set id="emails" set-class="java.util.TreeSet">
<value>[email protected] (英語) </value>
<value>[email protected] (英語) </value>
<value>[email protected] (英語) </value>
<value>[email protected] (英語) </value>
</util:set>set-class 属性が指定されていない場合、コンテナーは Set 実装を選択します。
aop スキーマ
aop タグは、Spring 独自のプロキシベースの AOP フレームワークや、AspectJ AOP フレームワークとの Spring の統合など、Spring でのすべての AOP の構成を処理します。これらのタグは、Spring によるアスペクト指向プログラミングというタイトルの章で包括的にカバーされています。
完全を期すために、aop スキーマのタグを使用するには、Spring XML 構成ファイルの先頭に次のプリアンブルが必要です(スニペットのテキストは正しいスキーマを参照するため、aop 名前空間のタグはあなたに利用可能):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- bean definitions here -->
</beans>context スキーマ
context タグは、接続機能に関連する ApplicationContext 構成を処理します。つまり、通常、エンドユーザーにとって重要な Bean ではなく、BeanfactoryPostProcessors などの Spring で多くの「うなり」をする Bean です。次のスニペットは、context 名前空間の要素が利用できるように正しいスキーマを参照しています。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- bean definitions here -->
</beans><property-placeholder/> を使用する
この要素は、${…} プレースホルダーの置換をアクティブにします。これは、指定されたプロパティファイルに対して解決されます(Spring リソースの場所として)。この要素は、PropertySourcesPlaceholderConfigurer をセットアップする便利なメカニズムです。特定の PropertySourcesPlaceholderConfigurer セットアップをさらに制御する必要がある場合は、自分で Bean として明示的に定義できます。
必要なプロパティを持つ特定のアプリケーションに対して、そのような要素を 1 つだけ定義する必要があります。個別のプレースホルダー構文 ( 置換に使用されるプロパティのソースをモジュール化する必要がある場合は、複数のプロパティプレースホルダーを作成しないでください。むしろ、各モジュールは |
<annotation-config/> を使用する
この要素は、Spring インフラストラクチャをアクティブにして、Bean クラスのアノテーションを検出します。
Spring の
@Configurationモデル@Autowired/@Inject、@Value、@LookupJSR-250 の
@Resource、@PostConstruct、@PreDestroy(使用可能な場合)JAX-WS の
@WebServiceRefおよび EJB3 の@EJB(使用可能な場合)JPA の
@PersistenceContextおよび@PersistenceUnit(使用可能な場合)Spring の
@EventListener
または、これらのアノテーションに対して個々の BeanPostProcessors を明示的にアクティブにすることを選択できます。
この要素は、Spring の @Transactional アノテーションの処理をアクティブにしません。その目的には <tx:annotation-driven/> 要素を使用できます。同様に、Spring のキャッシュアノテーションも明示的に有効にする必要があります。 |
<component-scan/> を使用する
この要素については、「アノテーションベースのコンテナー構成」のセクションで詳しく説明します。
<load-time-weaver/> を使用する
この要素については、Spring Framework の AspectJ を使用したロード時のウィービングに関するセクションで詳しく説明されています。
<spring-configured/> を使用する
この要素については、AspectJ を使用して Spring でドメインオブジェクトに依存関係を注入するセクションで詳しく説明します。
<mbean-export/> を使用する
この要素については、「アノテーションベースの MBean エクスポートの構成」セクションで詳しく説明します。
Beans スキーマ
最後になりますが、beans スキーマには要素があります。これらの要素は、フレームワークの黎明期から Spring に組み込まれていました。beans スキーマのさまざまな要素の例は、依存関係と構成の詳細 (実際、その章全体) で非常に包括的にカバーされているため、ここでは示しません。
<bean/> XML 定義にゼロ以上のキーと値のペアを追加できることに注意してください。この追加のメタデータを使用して何が行われるかは、完全に独自のカスタムロジック次第です(したがって、XML スキーマオーサリングというタイトルの付録に従って独自のカスタム要素を記述する場合にのみ通常使用されます)。
次の例は、周囲の <bean/> のコンテキストで <meta/> 要素を示しています(それを解釈するためのロジックがなければ、メタデータは事実上役に立たないことに注意してください)。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="foo" class="x.y.Foo">
<meta key="cacheName" value="foo"/> (1)
<property name="name" value="Rick"/>
</bean>
</beans>| 1 | これは meta 要素の例です |
上記の例の場合、Bean 定義を消費し、提供されたメタデータを使用するキャッシュインフラストラクチャを設定するロジックがあると想定できます。