Spring 中Bean的自动装配六种模式其三及常见异常
Spring IoC容器可以自动装配(autowire)相互协作bean之间的关联关系。因此,如果可能的话,可以自动让Spring通过检查BeanFactory中的内容,来替我们指定bean的协作者(其他被依赖的bean)。autowire一共有六种类型。由于autowire可以针对单个bean进行设置,因此可以让有些bean使用autowire,有些bean不采用。autowire的方便之处在减少或者消除属性或构造器参数的设置,这样可以给我们的配置文件减减肥![2] 在xml配置文件中,可以在<bean/>元素中使用autowire属性指定:
模式
|
说明
|
Bytype
|
如果容器中存在一个与指定属性类型相同的bean,那么将与该属性自动装配。如果存在多个该类型的bean,那么将会抛出异常,并指出不能使用byType方式进行自动装配。若没有找到相匹配的bean,则什么事都不发生,属性也不会被设置。如果你不希望这样,那么可以通过设置dependency-check="objects"让Spring抛出异常。
|
下来我们就用案例来证明一下:准备3个类:
public class AddressServiceImpl {
/**住址*/
private String address;
public void setAddress(String address){
this.address=address;
}
}
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;
}
}
public class EmpServiceImpl {
/**公司地址*/
private AddressServiceImpl companyAddress;
public void setCompanyAddress(AddressServiceImpl companyAddress){
this.companyAddress=companyAddress;
}
}
byType值bytype.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.0.xsd">
<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" />
</beans>
注意异常:
<beanid="addressServiceImpl"class="cn.csdn.service.AddressServiceImpl" scope="singleton"/>
//homeAddressServiceImpl是继承addressServiceImpl,所以他们是同一类型!
<!-- 当有多个相同类型的bean时,会出现bug如下:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'empServiceImpl' defined in file [D:\Workspaces\MyEclipse 8.6\20110419_01\bin\applicationContext.xml]: Unsatisfied dependency expressed through bean property 'companyAddress': : No unique bean of type [cn.csdn.service.AddressServiceImpl] is defined: expected single matching bean but found 2: [homeAddressServiceImpl, addressServiceImpl]; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [cn.csdn.service.AddressServiceImpl] is defined: expected single matching bean but found 2: [homeAddressServiceImpl, addressServiceImpl]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1091)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:982)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)..........................
-->
测试类:(junit测试)
public class App {
@Test
public void test(){
ApplicationContext ac=new ClassPathXmlApplicationContext("classpath:byName.xml"); EmpServiceImpl emp = (EmpServiceImpl) ac.getBean("empServiceImpl");
}
}
注意:
byType
在使用的过程中必须保证bean能够初始化,否则的话会出现bug
如果有默认的无参数的构造器就不需要多余的配置
如果有带有参数的构造器,那在bean的配置中必须配置器初始化的参数 或者在bean中添加无参数的构造器
<!--EndFragment-->
分享到:
相关推荐
ean的自动装配,有4种 (1)no:不做任何操作 (2)byName:根据属性 名 自动装配,设值注入 <bean id="xxx" class="xxx" ></bean> (3)byType:根据属性 类型 自动装配,相同类型多个会抛出异常,设值注入 <bean...
bean加入spring容器管理的方式,bean加入applicationcontext容器的方式
Spring中的自动装配有哪些限制?.docx Spring中的自动装配有哪些限制?.docx Spring中的自动装配有哪些限制?.docx
本项目为Java项目,使用Spring实现自动装配,根据类型方式实现自动装配。适合初学者了解Spring不同装配方式。
spring的自动装配
众所周知Spring里面的bean就类似是定义的一个组件,而这个组件的作用就是实现某个功能的,下面这篇文章主要给大家介绍了关于spring入门教程之bean继承与自动装配的相关资料,需要的朋友可以参考借鉴,下面随着小编来...
1.4 装配Bean-自动装配(重要!) Spring从两个角度来实现自动化装配: 组件扫描(component scanning):Spring会自动发现应用上下文中所创建的bean。 自动装配(autowiring):Spring自动满足bean之间的依赖。 1 ...
主要给大家介绍了关于spring装配bean的3种方式,文中通过示例代码介绍的非常详细,对大家的学习或者使用Spring具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
本篇文章主要介绍了Spring 中如何控制2个bean中的初始化顺序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
Spring自动装配解析,具体效果和过程看博文 http://blog.csdn.net/evankaka/article/details/44836069
spring 获取bean spring 获取bean spring 获取bean spring 获取bean spring 获取bean spring 获取bean spring 获取bean spring 获取bean
spring中的自动装配实例byName、byType,两种装配方式的实例。有需要学习的朋友可以看一下。
Spring Bean创建初始化流程
博客地址:https://blog.csdn.net/u010476739/article/details/76732201 spring装配bean的方式实例
Spring bean 一般通过配置文件和注解进行加载,如果要实现jar或class文件,动态实现spring bean 的动态加载,并通过UrlClassLoader完成jar和class文件的加载。可以实现jar的热替换。spring的bean动态加载则需要对...
spring自动扫描和管理Bean的示例
spring自动装配项目struts2,使用了struts2 ,spring2.5 ,hibernate,和spring 自动装配
spring bean配置 运行环境:eclipse 构建工具:maven 不提供maven构建,maven用来解决jar包的依赖
spring动态向容器中添加bean和删除指定bean,不需要重启应用
Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架,如何在程序中获取Spring配置的bean呢?下面通过本文给大家介绍Java中Spring获取bean方法小结,对spring获取bean方法相关知识感兴趣的朋友一起学习...