鍍金池/ 教程/ Java/ Spring依賴檢查
Spring EL hello world實(shí)例
Spring使用@Autowired注解自動裝配
MapFactoryBean實(shí)例
Bean InitializingBean和DisposableBean實(shí)例
Spring使用@Required注解依賴檢查
安裝Spring工具套件到Eclipse
CGLIB is required to process @Configuration classes
Spring EL Arrays, Lists, Maps實(shí)例
Spring由類型(Type)自動裝配
Spring在bean配置文件中定義電子郵件模板
Spring發(fā)送帶附件郵件
ListFactoryBean實(shí)例
Struts 2 + Spring + Hibernate集成實(shí)例
Spring bean引用實(shí)例
Spring JdbcTemplate batchUpdate()實(shí)例
Spring SetFactoryBean實(shí)例
Spring通過setter方法注入
Spring EL hello world實(shí)例
在bean配置文件中的電子郵件模板
Spring發(fā)送帶附件的Email
Spring EL 方法調(diào)用實(shí)例
Spring自動裝配Beans
Spring由構(gòu)造方法自動裝配
Spring使用@Required注解依賴檢查
Spring SimpleJdbcTemplate查詢示例
自定義@Required-style注解
Spring+JDBC實(shí)例
Spring EL三元運(yùn)算(if-then-else)實(shí)例
JdbcTemplate+JdbcDaoSupport實(shí)例
Spring自動掃描組件
Spring EL方法調(diào)用實(shí)例
Spring自動裝配Beans
Spring EL bean引用實(shí)例
Spring依賴注入(DI)
Spring EL Lists,Maps實(shí)例
Spring通過構(gòu)造方法依賴注入
通過MailSender發(fā)送電子郵件
Spring AOP實(shí)例(Pointcut, Advisor)
Spring EL運(yùn)算符實(shí)例
Spring JavaConfig實(shí)例
Spring依賴注入 (DI)
Spring EL bean引用實(shí)例
Spring由AutoDetect自動裝配
Spring由構(gòu)造方法自動裝配
Spring JdbcTemplate查詢實(shí)例
Spring SimpleJdbcTemplate batchUpdate()實(shí)例
Spring+JDBC實(shí)例
如何注入值到Spring bean屬性
Spring SimpleJdbcTemplate類命名參數(shù)實(shí)例
Spring PropertyPlaceholderConfigurer實(shí)例
Spring JdbcTemplate batchUpdate() 實(shí)例
Spring EL與ExpressionParser測試
Spring內(nèi)部Bean實(shí)例
加載多個Spring bean配置文件
Spring+Hibernate+MySql實(shí)例(注解)
Spring AOP+AspectJ 在XML配置實(shí)例
Spring自動代理創(chuàng)建者實(shí)例
Spring EL正則表達(dá)式實(shí)例
測試 Spring EL與ExpressionParser
java.lang.ClassNotFoundException: org.exolab.castor.xml.XMLExcep
Spring PropertyPlaceholderConfigurer實(shí)例
Spring bean配置繼承
Spring使用Setter依賴注入
Spring自動裝配@Qualifier實(shí)例
Spring依賴檢查
Spring hello world實(shí)例
Spring依賴注入servlet會話監(jiān)聽器
Spring自定義@Required-style注解
Spring bean作用域
Spring AOP攔截器的序列
注入值到Spring Bean的屬性
Spring JdbcTemplate+JdbcDaoSupport實(shí)例
Spring教程
Spring自動裝配@Qualifier實(shí)例
Spring MapFactoryBean例子
Spring Bean作用域?qū)嵗?/span>
Cannot proxy target class because CGLIB2 is not available
Spring AOP攔截器的順序
Spring集合(List, Set, Map, and Properties)實(shí)例
Spring SimpleJdbcTemplate類命名參數(shù)實(shí)例
Spring通過自動檢測自動裝配
Spring+Hibernate+MySql實(shí)例
Spring JavaConfig @Import實(shí)例
訪問MessageSource的bean(MessageSourceAware)
Spring JdbcTemplate查詢實(shí)例
Spring JavaConfig @Import實(shí)例
Spring Object/XML映射實(shí)例
Spring Bean引用例子
Spring AOP+AspectJ注解實(shí)例
java.lang.ClassNotFoundException: org.apache.xml.serialize.XMLSe
Spring依賴檢查
Spring JavaConfig實(shí)例
Spring AOP+AspectJ注解實(shí)例
Spring bean配置繼承
Spring自動掃描組件
自動代理創(chuàng)建者實(shí)例
Spring Bean init-method 和 destroy-method實(shí)例
Spring通過構(gòu)造方法注入
Spring過濾器組件自動掃描
Spring構(gòu)造方法注入類型歧義
Spring+JDBC實(shí)例
Spring EL正則表達(dá)式實(shí)例
Spring通過Gmail SMTP服務(wù)器MailSender發(fā)送電子郵件
Spring AOP+AspectJ在XML配置實(shí)例
SetFactoryBean實(shí)例
Spring注入日期到bean屬性-CustomDateEditor
Spring AOP通知實(shí)例 – Advice
Spring使用@Autowired注解自動裝配
Spring AOP實(shí)例(Pointcut,Advisor)
Spring AOP在Hibernate的事務(wù)管理
Spring初學(xué)快速入門
Spring內(nèi)部bean實(shí)例
Spring bean加載多個配置文件
Spring + Hibernate+ MySql注解實(shí)例
Spring @PostConstruct和@PreDestroy實(shí)例
Spring依賴注入servlet會話監(jiān)聽器
Spring ListFactoryBean實(shí)例
Spring松耦合的實(shí)例
Spring過濾器組件自動掃描
Spring按名稱(Name)自動裝配
Spring AOP通知實(shí)例(Advice)
Spring AOP在Hibernate事務(wù)管理
Bean init-method和destroy-method實(shí)例
Spring EL操作符實(shí)例
Spring注入日期到bean屬性(CustomDateEditor)
資源加載程序的getResource()示例
Bean @PostConstruct和@PreDestroy實(shí)例
Struts2+Spring集成實(shí)例
Spring集合 (List,Set,Map,Properties) 實(shí)例
Spring按類型(Type)自動裝配
Spring由名稱(Name)自動裝配
Spring SimpleJdbcTemplate batchUpdate()實(shí)例
Spring hello world實(shí)例
Spring Bean InitializingBean和DisposableBean實(shí)例
Spring構(gòu)造方法注入類型歧義
Spring EL三元操作符(if-then-else)實(shí)例
Spring+Hibernate+MySql實(shí)例
Spring松耦合實(shí)例

Spring依賴檢查

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

依賴檢查模式

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

注:默認(rèn)模式是 none

示例

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

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

	//getter and setter methods
}
package com.yiibai.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.yiibai.common.Customer" >
		<property name="action" value="buy" />
	</bean>

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

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

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.yiibai.common.Customer" 
         dependency-check="simple">

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

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

</beans>
在“type”屬性(基本類型或集合類型),如尚未設(shè)置,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.yiibai.common.Customer" 
         dependency-check="objects">

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

	<bean id="PersonBean" class="com.yiibai.common.Person">
		<property name="name" value="yiibai" />
		<property name="address" value="address ABC" />
		<property name="age" value="29" />
	</bean>
	
</beans>
在'person'屬性(對象類型),尚未設(shè)置,一個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.yiibai.common.Customer" 
         dependency-check="all">

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

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

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

全局默認(rèn)的依賴檢查

明確定義的依賴檢查模式,每個Bean配置繁瑣且容易出錯,可以在<beans>根元素設(shè)置一個默認(rèn)的依賴性檢查屬性強(qiáng)制<beans>根元素內(nèi)聲明的整個bean類適用此規(guī)則。然而,這根默認(rèn)模式將通過一個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.yiibai.common.Customer">
		<property name="action" value="buy" />
		<property name="type" value="1" />
	</bean>

	<bean id="PersonBean" class="com.yiibai.common.Person">
		<property name="name" value="yiibai" />
		<property name="address" value="address ABC" />
		<property name="age" value="29" />
	</bean>
	
</beans>
在這個配置文件中聲明所有的Bean類默都是“all”依賴檢查模式。
@Required 注解
在大多數(shù)情況下,你只需要確保特定屬性已經(jīng)設(shè)置,但有一定是所有的類型(原始,集合或?qū)ο?屬性。