Spring依賴檢查

在Spring中,可以使用依賴檢查功能,以確保所要求的屬性可設置或者注入。

依賴檢查模式

4個依賴檢查支持的模式:
  • none – 沒有依賴檢查,這是默認的模式。
  • simple – 如果基本類型(int, long,double…)和集合類型(map, list..)的任何屬性都沒有設置,UnsatisfiedDependencyException將被拋出。
  • objects – 如果對象類型的任何屬性都沒有設置,UnsatisfiedDependencyException將被拋出。
  • all – 如果任何類型的任何屬性都沒有被設置,UnsatisfiedDependencyException將被拋出。

注:默認模式是 none

示例

Customer和Person對象的示例。
package com.xuhuhu.common;

public class Customer
{
	private Person person;
	private int type;
	private String action;

	//getter and setter methods
}
package com.xuhuhu.common;

public class Person
{
	private String name;
	private String address;
	private int age;

	//getter and setter methods
}

1. none 依賴檢查

Spring bean配置檔使用 “none” 依賴檢查模式。
<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.xuhuhu.common.Customer" >
		<property name="action" value="buy" />
	</bean>

	<bean id="PersonBean" class="com.xuhuhu.common.Person">
		<property name="name" value="zaixian" />
		<property name="address" value="address ABC" />
		<property name="age" value="29" />
	</bean>

</beans>
如果沒有明確定義的依賴檢查模式,其默認為“none”。沒有依賴檢查將執行。

2. simple 依賴檢查

Spring bean的配置檔使用“simple”依賴檢查模式。
<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.xuhuhu.common.Customer"
         dependency-check="simple">

		<property name="person" ref="PersonBean" />
		<property name="action" value="buy" />
	</bean>

	<bean id="PersonBean" class="com.xuhuhu.common.Person">
		<property name="name" value="zaixian" />
		<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. objects 依賴檢查

Spring bean配置檔的 “objects”依賴檢查模式。
<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.xuhuhu.common.Customer"
         dependency-check="objects">

		<property name="action" value="buy" />
		<property name="type" value="1" />
	</bean>

	<bean id="PersonBean" class="com.xuhuhu.common.Person">
		<property name="name" value="zaixian" />
		<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配置檔的“all”依賴檢查模式。
<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.xuhuhu.common.Customer"
         dependency-check="all">

		<property name="action" value="buy" />
	</bean>

	<bean id="PersonBean" class="com.xuhuhu.common.Person">
		<property name="name" value="zaixian" />
		<property name="address" value="address ABC" />
		<property name="age" value="29" />
	</bean>

</beans>
對“simple”和“objects”模式的組合,如果有的話類型(原型,集合和對象)的任何屬性都沒有設置,一個UnsatisfiedDependencyException將被拋出。

全局默認的依賴檢查

明確定義的依賴檢查模式,每個Bean配置繁瑣且容易出錯,可以在<beans>根元素設置一個默認的依賴性檢查屬性強制<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.xuhuhu.common.Customer">
		<property name="action" value="buy" />
		<property name="type" value="1" />
	</bean>

	<bean id="PersonBean" class="com.xuhuhu.common.Person">
		<property name="name" value="zaixian" />
		<property name="address" value="address ABC" />
		<property name="age" value="29" />
	</bean>

</beans>
在這個配置檔中聲明所有的Bean類默都是“all”依賴檢查模式。
@Required 注解
在大多數情況下,你只需要確保特定屬性已經設置,但有一定是所有的類型(原始,集合或對象)屬性。

上一篇: Spring bean配置繼承 下一篇: Spring使用@Required注解依賴檢查