在Spring中,您可以使用依赖项检查功能来确保已设置或注入了所需的属性。
依赖检查模式
支持4种依赖项检查模式:
- none –不进行依赖检查。
- 简单–如果未设置任何原始类型(int,long,double…)和集合类型(map,list ..)的属性,则将引发UnsatisfiedDependencyException。
- objects –如果未设置任何对象类型的属性,则将引发UnsatisfiedDependencyException。
- all –如果未设置任何类型的任何属性,则将发生UnsatisfiedDependencyException
将被抛出。
PS默认模式为无
例
演示的客户和个人对象。
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>
如果您未明确定义依赖项检查模式,则默认为“无”。 不会执行依赖检查。
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都为“所有”依赖项检查模式。
@必填注释
在大多数情况下,您只需要确保已设置了特定的属性,而不是特定类型(原始,集合或对象)的所有属性。 @Required批注可以强制执行此检查, 请参见detail 。
翻译自: https://mkyong.com/spring/spring-properties-dependency-checking/