体验Spring(七)---依赖检查

依赖检查:

dependency-check=’all’||’simple’||’none’||’object’

Spring除了能对容器中bean的依赖设置进行检查外。还可以检查bean定义中实际属性值的设置,当然也包括采用自动装配方式设置属性值的检查。

Domain包中的Emp.java
public class Emp implements Serializable {
//没有任何属性和方法
}
Service包中的AddressServiceImpl.java
public class AddressServiceImpl {

}
Service包中的EmpServiceImpl.java
public class EmpServiceImpl {
	private  String name;
	private  Integer age;
    //Integer与String都是原始数据类型
	private List<Emp> list;
//生成set()与get()方法

	public void setEmail(String email) {
	}
}
Check.xml
<?xml version="1.0" encoding="UTF-8"?>
<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="emp " class="cn.csdn.domain.Emp" scope="singleton" />
	<bean id="empServiceImpl" class="cn.csdn.service.EmpServiceImpl" scope="singleton" dependency-check="all">
		<property name="name">
			<value>DXL</value>
		</property>
		
		<property name="email">
			<value>[email protected]</value>
		</property>
		
		<property name="list">
			<list>
				<ref bean="emp" />
			</list>
		</property>
	</bean>

</beans>
<!-- 依赖检查主要是检查bean中的Setter放法的属性是否在配置文件中设置property属性 如果没有设置就会出现bug
Simple  只对原始数据类型进行检查例如:Integer和String
Object  只能验证集合和对象(bean)       list类型需要使用Object进行检查 
All      验证所有
none    没有依赖检查,如果Bean的属性没有值的话可以不用设置
 -->

 

若在empServiceImpl.java中没有email属性只有emailset()方法,dependency-check="all"dependency-check="simple"也会验证通过。

age的类型为int类型则不能通过验证,原因是int类型并不是原始数据类型。

 

 

猜你喜欢

转载自dxl-xiaoli.iteye.com/blog/1041731