Spring 专题
专题目录
您的位置:java > Spring专题 > Spring依赖检查
Spring依赖检查
作者:--    发布时间:2019-11-20
在spring中,可以使用依赖检查功能,以确保所要求的属性可设置或者注入。

依赖检查模式

4个依赖检查支持的模式:
  • none – 没有依赖检查,这是默认的模式。
  • simple – 如果基本类型(int, long,double…)和集合类型(map, list..)的任何属性都没有设置,unsatisfieddependencyexception将被抛出。
  • objects – 如果对象类型的任何属性都没有设置,unsatisfieddependencyexception将被抛出。
  • all – 如果任何类型的任何属性都没有被设置,unsatisfieddependencyexception将被抛出。

注:默认模式是 none

示例

customer和person对象的示例。
package com.h3.common;

public class customer 
{
	private person person;
	private int type;
	private string action;

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

	<bean id="personbean" class="com.h3.common.person">
		<property name="name" value="h3" />
		<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.h3.common.customer" 
         dependency-check="simple">

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

	<bean id="personbean" class="com.h3.common.person">
		<property name="name" value="h3" />
		<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.h3.common.customer" 
         dependency-check="objects">

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

	<bean id="personbean" class="com.h3.common.person">
		<property name="name" value="h3" />
		<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.h3.common.customer" 
         dependency-check="all">

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

	<bean id="personbean" class="com.h3.common.person">
		<property name="name" value="h3" />
		<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.h3.common.customer">
		<property name="action" value="buy" />
		<property name="type" value="1" />
	</bean>

	<bean id="personbean" class="com.h3.common.person">
		<property name="name" value="h3" />
		<property name="address" value="address abc" />
		<property name="age" value="29" />
	</bean>
	
</beans>
在这个配置文件中声明所有的bean类默都是“all”依赖检查模式。
@required 注解
在大多数情况下,你只需要确保特定属性已经设置,但有一定是所有的类型(原始,集合或对象)属性。

网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册