依赖检查
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.
本文详细介绍了Spring框架中Bean的依赖检查模式,包括none、simple、object和all四种模式,并通过实例展示了不同模式下如何配置XML文件来实现依赖检查。
1158

被折叠的 条评论
为什么被折叠?



