Spring的依赖检查功能能够帮助检查在一个Bean上的所有特定类型属性是否都已经设置。只要在dependency-check的属性中指定寄来检查模式就可以了。
依赖检查的几种模式:
1、none:不执行任何依赖检查。任何属性都可以保持未设置状态,为默认;
2、simple:如果任何简单类型(原始和集合类型)的属性未设置,将抛出UnsatisfiedDependencyexception异常;
3、objects:如果任何对象类型属性都没有设置,将抛出UnsatisfiedDependencyexception异常;
4、all:如果任何类型的属性未设置,将抛出UnsatisfiedDependencyexception异常。
可以设置<beans>根元素的default-dependency-check属性来改变。
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
">
<!-- 配置一个Bean -->
<bean name="studentBean" class="com.lkt.entity.Student" scope="prototype" dependency-check="all">
<property name="clazz" ref="classBean"></property>
<property name="password" value="zhangsan"></property>
<property name="userName" value="zhangsan"></property>
</bean>
<bean name="classBean" class="com.lkt.entity.Clazz" scope="prototype">
<property name="className" value="一年三班"></property>
</bean>
</beans>
因为有个属性realName没有设置,所有在执行后,会报错误:
Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'studentBean' defined in class path resource [applicationContext.xml]: Unsatisfied dependency expressed through bean property 'realName': Set this property value or disable dependency checking for this bean.
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.checkDependencies(AbstractAutowireCapableBeanFactory.java:1240)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1063)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:309)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1004)
at com.lkt.entity.Test.main(Test.java:11)
Spring的依赖检查功能灵活性不够,所有提供了注解的方式
RequiredAnnotationBeanPostProcessor是一个Spring bean 后处理器,检查带有@Required注解的所有bean属性是否设置。
使用注解要在添加一个context:annotation-config元素
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<!-- 添加注解功能 -->
<context:annotation-config/>
<!-- 配置一个Bean -->
<bean name="studentBean" class="com.lkt.entity.Student" scope="prototype" >
<property name="clazz" ref="classBean"></property>
<property name="password" value="zhangsan"></property>
<property name="userName" value="zhangsan"></property>
</bean>
<bean name="classBean" class="com.lkt.entity.Clazz" scope="prototype">
<property name="className" value="一年三班"></property>
</bean>
</beans>
在实体类中的属性中添加@Required注解
package com.lkt.entity;
import org.springframework.beans.factory.annotation.Required;
public class Student {
private String userName;
private String password;
private String realName;
private Clazz clazz;
public Clazz getClazz() {
return clazz;
}
public void setClazz(Clazz clazz) {
this.clazz = clazz;
}
public Student() {
// TODO Auto-generated constructor stub
}
public Student(String userName,String password,String realName) {
this.userName=userName;
this.password=password;
this.realName=realName;
}
@Override
public String toString() {
return "userName:"+userName+" password:"+password+" realName:"+realName;
}
public String getUserName() {
return userName;
}
@Required
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
@Required
public void setPassword(String password) {
this.password = password;
}
public String getRealName() {
return realName;
}
@Required
public void setRealName(String realName) {
this.realName = realName;
}
}