依赖检查
Spring中的Bean中有种依赖检查模式:none、simple、object、all
1、 none:不进行依赖检查
<?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.0.xsd"> <bean id="emp" class="cn.csdn.domain.Emp" scope="singleton"/> <bean id="empServiceImpl" class="cn.csdn.service.EmpServiceImpl" scope="singleton" dependency-check="none"> <property name="name"> <value>kouxiaolin</value> </property> <property name="email"> <value>kouxiaolin@qq.com</value> </property> <property name="list"> <list> <ref bean="emp"/> </list> </property> </bean> </beans>
2、
simplesimple
:
对于原始类型及集合(除协作者外的一切东西)执行依赖检查
3、 object:对依赖的对象进行检查(仅对协作者执行依赖检查)
<?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.0.xsd"> <bean id="emp" class="cn.csdn.domain.Emp" scope="singleton"/> <bean id="empServiceImpl" class="cn.csdn.service.EmpServiceImpl" scope="singleton" dependency-check="object"> <property name="name"> <value>kouxiaolin</value> </property> <property name="email"> <value>kouxiaolin@qq.com</value> </property> <property name="list"> <list> <ref bean="emp"/> </list> </property> </bean> </beans>
4、 all:对全部属性进行检查
<?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.0.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>kouxiaolin</value> </property> <property name="email"> <value>kouxiaolin@qq.com</value> </property> <property name="list"> <list> <ref bean="emp"/>//引用上一个bean用<ref bean=”emp”/> </list> </property> </bean> </beans>
注:
bean 类中如果没有属性只有 set 方法会不会出错啊?答案是没有错,因为依赖检查主要是检查bean中的setter方法的属性是否在配置文件中设置property属性
Bean
类
备注:
依赖检查主要是检查bean中的setter方法的属性是否在配置文件中设置property属性如果没有设置就会出现bug:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'empServiceImpl' defined in file [D:\Workspaces\MyEclipse 8.6\20110419_04\bin\check.xml]: Unsatisfied dependency expressed through bean property 'email': Set this property value or disable dependency checking for this bean.