Springプロパティの依存関係チェック
Springでは、依存性チェック機能を使用して、必要なプロパティが設定または注入されていることを確認できます。
依存性チェックモード
4つの依存性検査モードがサポートされています:
コレクションの型(map、list ..)が設定されていない場合、UnsatisfiedDependencyExceptionがスローされます。
-
オブジェクト – オブジェクトタイプのプロパティが設定されていない場合、
UnsatisfiedDependencyExceptionがスローされます。
-
all – 任意のタイプのプロパティが設定されていない場合、
UnsatisfiedDependencyExceptionがスローされます。
P.Sデフォルトモードはnone
です
例
デモ用のCustomerオブジェクトとPersonオブジェクト。
package com.mkyong.common;
public class Customer
{
private Person person;
private int type;
private String action;
//getter and setter methods
}
package com.mkyong.common;
public class Person
{
private String name;
private String address;
private int age;
//getter and setter methods
}
1.なし依存性チェック
依存性検査モードが ‘none’のSpring Bean構成ファイル。
<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
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="CustomerBean" class="com.mkyong.common.Customer" >
<property name="action" value="buy"/>
</bean>
<bean id="PersonBean" class="com.mkyong.common.Person">
<property name="name" value="mkyong"/>
<property name="address" value="address ABC"/>
<property name="age" value="29"/>
</bean>
</beans>
依存関係チェックモードを明示的に定義しなかった場合は、デフォルトで ‘none’に設定されます。依存性チェックは実行されません。
2.簡単な依存性チェック
‘単純な’依存性検査モードを持つSpring Bean構成ファイル。
<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
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="CustomerBean" class="com.mkyong.common.Customer"
dependency-check="simple">
<property name="person" ref="PersonBean"/>
<property name="action" value="buy"/>
</bean>
<bean id="PersonBean" class="com.mkyong.common.Person">
<property name="name" value="mkyong"/>
<property name="address" value="address ABC"/>
<property name="age" value="29"/>
</bean>
</beans>
‘type’プロパティ(プリミティブ型またはコレクション型)が設定されていない場合、
UnsatisfiedDependencyException
がスローされます。
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'CustomerBean' defined in class path resource[config/Spring-Customer.xml]: Unsatisfied dependency expressed through bean property 'type': Set this property value or disable dependency checking for this bean.
3.オブジェクトの依存性チェック
‘オブジェクト’依存性検査モードを持つSpring Bean構成ファイル。
<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
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="CustomerBean" class="com.mkyong.common.Customer"
dependency-check="objects">
<property name="action" value="buy"/>
<property name="type" value="1"/>
</bean>
<bean id="PersonBean" class="com.mkyong.common.Person">
<property name="name" value="mkyong"/>
<property name="address" value="address ABC"/>
<property name="age" value="29"/>
</bean>
</beans>
‘person’プロパティ(オブジェクト型)が設定されていない場合、
UnsatisfiedDependencyException
がスローされます。
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'CustomerBean' defined in class path resource[config/Spring-Customer.xml]: Unsatisfied dependency expressed through bean property 'person': Set this property value or disable dependency checking for this bean.
4.すべての依存関係チェック
‘all’依存チェックモードのSpring Bean設定ファイル。
<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
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="CustomerBean" class="com.mkyong.common.Customer"
dependency-check="all">
<property name="action" value="buy"/>
</bean>
<bean id="PersonBean" class="com.mkyong.common.Person">
<property name="name" value="mkyong"/>
<property name="address" value="address ABC"/>
<property name="age" value="29"/>
</bean>
</beans>
「シンプル」と「オブジェクト」モードの組み合わせは、任意の型(プリミティブ、コレクション、およびオブジェクト)のプロパティが設定されていない場合、
UnsatisfiedDependencyException
がスローされます。
グローバルデフォルト依存性チェック
すべてのBeanの依存性チェックモードを明示的に定義するのは面倒でエラーが発生しやすいため、<beans>ルート要素でdefault-dependency-check属性を設定して<beans>ルート要素内で宣言されたBean全体にこのルールを適用させることができます。ただし、このルートのデフォルトモードは、指定されていればBeanの独自のモードで上書きされます。
<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
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
default-dependency-check="all">
<bean id="CustomerBean" class="com.mkyong.common.Customer">
<property name="action" value="buy"/>
<property name="type" value="1"/>
</bean>
<bean id="PersonBean" class="com.mkyong.common.Person">
<property name="name" value="mkyong"/>
<property name="address" value="address ABC"/>
<property name="age" value="29"/>
</bean>
</beans>
この設定ファイルで宣言されたすべてのBeanは、デフォルトで ‘all’依存性検査モードになっています。
-
@必須アノテーション** ほとんどのシナリオでは、特定のプロパティ(プリミティブ、コレクションまたはオブジェクト)のすべてのプロパティではなく、特定のプロパティが設定されていることを確認する必要があります。 @Required Annotationは、このチェックを強制することができます://spring/spring-dependency-with-required-annotation/[詳細を参照]。