Spring自动装配模式三:byType的解析
来源:互联网 发布:证大淘宝达人贷 编辑:程序博客网 时间:2024/06/13 13:29
如果容器中存在一个与指定属性类型相同的bean,那么将与该属性自动装配。如果存在多个该类型的bean,那么将会抛出异常,并指出不能使用byType方式进行自动装配。若没有找到相匹配的bean,则什么事都不发生,属性也不会被设置。如果你不希望这样,那么可以通过设置dependency-check="objects"让Spring抛出异常。
配置文件如下:
<bean id="homeAddressServiceImpl" class="cn.csdn.service.HomeAddressServiceImpl"
scope="singleton">
<property name="address">
<value>北京海淀上地软件园</value>
</property>
</bean>
<bean id="empServiceImpl" class="cn.csdn.service.EmpServiceImpl"
scope="singleton" autowire="byType" />
cn.csdn.service.HomeAddressServiceImpl代码
public class HomeAddressServiceImpl extends AddressServiceImpl {
private String address;
public void setAddress(String address){
this.address=address;
}
public HomeAddressServiceImpl() {
super();
}
public HomeAddressServiceImpl(String address){
this.address=address;
}
}
cn.csdn.service.EmpServiceImpl 代码
package cn.csdn.service;
public class EmpServiceImpl {
private HomeAddressServiceImpl homeAddressServiceImpl;
public void setHomeAddressServiceImpl(
HomeAddressServiceImpl homeAddressServiceImpl) {
this.homeAddressServiceImpl = homeAddressServiceImpl;
}
}
注意:类型匹配指的是class 中的Employee匹配
在配置文件中 使用byType类型时,相同类型的实例只能存在一个。 如果存在多个该类型的bean,那么将会抛出异常: org.springframework.beans.factory.UnsatisfiedDependencyException
原因是:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [spr.bean.Employee] is defined 并指出不能使用byType方式进行自动装配。
bytype: 类型相同可以匹配
父类与子类匹配的方式是:子类自动装载成父类的对象。
接口与实现类:实现类的对象可以自动装载到接口的对象。否则会出现空指针异常。
配置文件如下:
<bean id="homeAddressServiceImpl" class="cn.csdn.service.HomeAddressServiceImpl"
scope="singleton">
<property name="address">
<value>北京海淀上地软件园</value>
</property>
</bean>
<bean id="empServiceImpl" class="cn.csdn.service.EmpServiceImpl"
scope="singleton" autowire="byType" />
cn.csdn.service.HomeAddressServiceImpl代码
public class HomeAddressServiceImpl extends AddressServiceImpl {
private String address;
public void setAddress(String address){
this.address=address;
}
public HomeAddressServiceImpl() {
super();
}
public HomeAddressServiceImpl(String address){
this.address=address;
}
}
cn.csdn.service.EmpServiceImpl 代码
package cn.csdn.service;
public class EmpServiceImpl {
private HomeAddressServiceImpl homeAddressServiceImpl;
public void setHomeAddressServiceImpl(
HomeAddressServiceImpl homeAddressServiceImpl) {
this.homeAddressServiceImpl = homeAddressServiceImpl;
}
}
注意:类型匹配指的是class 中的Employee匹配
在配置文件中 使用byType类型时,相同类型的实例只能存在一个。 如果存在多个该类型的bean,那么将会抛出异常: org.springframework.beans.factory.UnsatisfiedDependencyException
原因是:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [spr.bean.Employee] is defined 并指出不能使用byType方式进行自动装配。
bytype: 类型相同可以匹配
父类与子类匹配的方式是:子类自动装载成父类的对象。
接口与实现类:实现类的对象可以自动装载到接口的对象。否则会出现空指针异常。
- Spring自动装配模式三:byType的解析
- Spring自动装配模式三:byType的解析
- Spring的自动装配(byName;byType)
- Spring的自动装配(byName;byType)
- Spring的自动装配(byName;byType)
- Spring 框架--byType自动装配
- Spring Autowire自动装配byName和byType
- Spring自动装配之byName和byType【Spring入门】
- spring 自动装配 default-autowire="byName/byType"
- spring 自动装配 default-autowire="byName/byType"
- spring 自动装配autowire=byName或byType
- spring 自动装配 default-autowire="byName/byType"
- Spring自动装配模式五:autodetect的解析
- Spring自动装配模式二:byName的解析
- Spring自动装配模式四:constructor的解析
- Spring自动装配模式一:no解析
- 玩转Spring----autowire的bytype 和byname 装配
- Spring自动装配解析
- 一个计算机专业学生几年的Java编程经验汇总
- VC++动态链接库(DLL)编程深入浅出
- exe文件关联修复
- 游船出租
- Java工程师可能不知道的那些FE潜规则
- Spring自动装配模式三:byType的解析
- 【OOADP 学习笔记】之继承
- UML中类之间的关系及其Java代码例子
- hoj 4006(大连网络赛一个最小堆问题)
- 老子的软件之道 - 道篇 17 平台及管理体系的层次
- Linux下Mount第二块硬盘
- STL里的list是双链表 有双向迭代器,不能随机访问
- c++ constructor
- 爱上写博客,写出自己喜欢的东西.--自勉