Spring学习(三)—— 自动装配案例分析
Spring_Autowiring collaborators
在Spring3.2.2中自动装配类型,分别为:no(default)(不采用自动装配)、byName,byType,constructor下面来分别介绍一下这些是如何自动装配的
<bean id="foo" class="...Foo" autowire="autowire type">
Mode |
Explanation |
no |
(Default) No autowiring. Bean references must be defined via a ref element. Changing the default setting is not recommended for larger deployments, because specifying collaborators explicitly gives greater control and clarity. To some extent, it documents the structure of a system. |
byName |
Autowiring by property name. Spring looks for a bean with the same name as the property that needs to be autowired. For example, if a bean definition is set to autowire by name, and it contains a master property (that is, it has a setMaster(..) method), Spring looks for a bean definition named master, and uses it to set the property. |
byType |
Allows a property to be autowired if exactly one bean of the property type exists in the container. If more than one exists, a fatal exception is thrown, which indicates that you may not use byType autowiring for that bean. If there are no matching beans, nothing happens; the property is not set. |
constructor |
Analogous to byType, but applies to constructor arguments. If there is not exactly one bean of the constructor argument type in the container, a fatal error is raised. |
案例分析:
1、创建CumputerBean类
- package www.csdn.spring.autowire.bean;
- public class CumputerBean {
- // 电脑名称
- private String name;
- public void setName(String name) {
- this.name = name;
- }
- @Override
- public String toString() {
- return "CumputerBean [name=" + name + "]";
- }
- }
2、创建DeptBean 类
- package www.csdn.spring.autowire.bean;
- public class DeptBean {
- //部门名称
- private String name;
- public void setName(String name) {
- this.name = name;
- }
- @Override
- public String toString() {
- return "DeptBean [name=" + name + "]";
- }
- }
3、创建EmployeeBean
- package www.csdn.spring.autowire.bean;
- public class EmployeeBean {
- private DeptBean deptBean;
- private CumputerBean cumputerBean;
- public void setDeptBean(DeptBean deptBean) {
- this.deptBean = deptBean;
- }
- public void setCumputerBean(CumputerBean cumputerBean) {
- this.cumputerBean = cumputerBean;
- }
- @Override
- public String toString() {
- return "EmployeeBean [deptBean=" + deptBean + ", cumputerBean="
- + cumputerBean + "]";
- }
- }
首先分析no、byName、byType的配置都是采用setter方法依赖注入实现的案例
1、no配置(通过ref=””引用需要的bean)
- <?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.xsd">
- <!-- 电脑bean -->
- <bean id="cumputerBean" class="www.csdn.spring.autowire.bean.CumputerBean">
- <property name="name" value="HP6325笔记本" />
- </bean>
- <!-- 部门bean -->
- <bean id="deptBean" class="www.csdn.spring.autowire.bean.DeptBean">
- <property name="name" value="CSDN教育事业部" />
- </bean>
- <!-- 员工bean 根据EmployeeBean中的 属性名称 去匹配-->
- <bean id="employeeBean" class="www.csdn.spring.autowire.bean.EmployeeBean">
- <property name="cumputerBean" ref="cumputerBean" />
- <property name="deptBean" ref="deptBean" />
- </bean>
- </beans>
2、byName配置(分析:会根据EmployeeBean中属性的名称 自动装配)
- <?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.xsd">
- <!-- 电脑bean -->
- <bean id="cumputerBean" class="www.csdn.spring.autowire.bean.CumputerBean">
- <property name="name" value="HP6325笔记本" />
- </bean>
- <!-- 部门bean -->
- <bean id="deptBean" class="www.csdn.spring.autowire.bean.DeptBean">
- <property name="name" value="CSDN教育事业部" />
- </bean>
- <!-- 员工bean-->
- <bean id="employeeBean" class="www.csdn.spring.autowire.bean.EmployeeBean" autowire="byName"/>
- </beans>
3、byType配置(分析:会根据EmployeeBean中属性的类型 自动装配)
- <?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.xsd">
- <!-- 电脑bean -->
- <bean id="cumputerBean" class="www.csdn.spring.autowire.bean.CumputerBean">
- <property name="name" value="HP6325笔记本" />
- </bean>
- <!-- 部门bean -->
- <bean id="deptBean" class="www.csdn.spring.autowire.bean.DeptBean">
- <property name="name" value="CSDN教育事业部" />
- </bean>
- <!-- 员工bean 根据EmployeeBean中的 属性名称 去匹配-->
- <bean id="employeeBean" class="www.csdn.spring.autowire.bean.EmployeeBean" autowire="byType"/>
- </beans>
注意:当根据byType类型装配时,当在容器内找到多个匹配的类型时会出现如下bug
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employeeBean' defined in class path resource [spring-byType.xml]: Unsatisfied dependency expressed through bean property 'deptBean': : No qualifying bean of type [www.csdn.spring.autowire.bean.DeptBean] is defined: expected single matching bean but found 2: deptBean,deptBean1; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [www.csdn.spring.autowire.bean.DeptBean] is defined: expected single matching bean but found 2: deptBean,deptBean1
4、Constructor(构造器参数根据byType类型匹配,自动装配)
首先修改EmployeeBean类 修改后代码如下:
- package www.csdn.spring.autowire.bean;
- public class EmployeeBean {
- private DeptBean deptBean;
- private CumputerBean cumputerBean;
- public EmployeeBean(DeptBean deptBean, CumputerBean cumputerBean) {
- super();
- this.deptBean = deptBean;
- this.cumputerBean = cumputerBean;
- }
- @Override
- public String toString() {
- return "EmployeeBean [deptBean=" + deptBean + ", cumputerBean="
- + cumputerBean + "]";
- }
- }
配置文件操作:
- <?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.xsd">
- <!-- 电脑bean -->
- <bean id="cumputerBean" class="www.csdn.spring.autowire.bean.CumputerBean">
- <property name="name" value="HP6325笔记本" />
- </bean>
- <!-- 部门bean -->
- <bean id="deptBean" class="www.csdn.spring.autowire.bean.DeptBean">
- <property name="name" value="CSDN教育事业部" />
- </bean>
- <!-- 员工bean 根据EmployeeBean中的 属性名称 bytype 去匹配 -->
- <bean id="employeeBean" class="www.csdn.spring.autowire.bean.EmployeeBean"
- autowire="constructor">
- </bean>
- </beans>
说明:
1、当构造器的参数类型在容器中找不全时。
比如:
配置文件中只配置了CumpterBean时
- <?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.xsd">
- <!-- 电脑bean -->
- <bean id="cumputerBean" class="www.csdn.spring.autowire.bean.CumputerBean">
- <property name="name" value="HP6325笔记本" />
- </bean>
- <!-- 员工bean 根据EmployeeBean中的 属性名称 bytype 去匹配 -->
- <bean id="employeeBean" class="www.csdn.spring.autowire.bean.EmployeeBean"
- autowire="constructor">
- </bean>
- </beans>
会出现如下bug:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employeeBean' defined in class path resource [spring-constructors.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [www.csdn.spring.autowire.bean.DeptBean]: : No qualifying bean of type [www.csdn.spring.autowire.bean.DeptBean] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [www.csdn.spring.autowire.bean.DeptBean] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
Caused by:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [www.csdn.spring.autowire.bean.DeptBean] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
2、当配置文件找到构造器参数的类型有多个的时候比如配置文件如下:
- <?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.xsd">
- <!-- 电脑bean -->
- <bean id="cumputerBean" class="www.csdn.spring.autowire.bean.CumputerBean">
- <property name="name" value="HP6325笔记本" />
- </bean>
- <!-- 部门bean -->
- <bean id="deptBean" class="www.csdn.spring.autowire.bean.DeptBean">
- <property name="name" value="CSDN教育事业部" />
- </bean>
- <!-- 部门bean -->
- <bean id="deptBean1" class="www.csdn.spring.autowire.bean.DeptBean">
- <property name="name" value="CSDN教育事业部" />
- </bean>
- <!-- 员工bean 根据EmployeeBean中的 属性名称 bytype 去匹配 -->
- <bean id="employeeBean" class="www.csdn.spring.autowire.bean.EmployeeBean"
- autowire="constructor">
- </bean>
- </beans>
说明:上面配置有两个同样类型的DeptBean但是不会出现bug,原因是在EmployeeBean中构造器接受的参数名称与deptBean一致。
3、当配置文件找到构造器参数的类型有多个的时候比如配置文件如下:
- <?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.xsd">
- <!-- 电脑bean -->
- <bean id="cumputerBean" class="www.csdn.spring.autowire.bean.CumputerBean">
- <property name="name" value="HP6325笔记本" />
- </bean>
- <!-- 部门bean -->
- <bean id="deptBean1" class="www.csdn.spring.autowire.bean.DeptBean">
- <property name="name" value="CSDN教育事业部" />
- </bean>
- <!-- 部门bean -->
- <bean id="deptBean2" class="www.csdn.spring.autowire.bean.DeptBean">
- <property name="name" value="CSDN教育事业部" />
- </bean>
- <!-- 员工bean 根据EmployeeBean中的 属性名称 bytype 去匹配 -->
- <bean id="employeeBean" class="www.csdn.spring.autowire.bean.EmployeeBean"
- autowire="constructor">
- </bean>
- </beans>
会出现如下bug(与byType的bug一致):
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employeeBean' defined in class path resource [spring-constructors.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [www.csdn.spring.autowire.bean.DeptBean]: : No qualifying bean of type [www.csdn.spring.autowire.bean.DeptBean] is defined: expected single matching bean but found 2: deptBean1,deptBean2; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [www.csdn.spring.autowire.bean.DeptBean] is defined: expected single matching bean but found 2: deptBean1,deptBean2
Spring学习(三)—— 自动装配案例分析的更多相关文章
- Spring学习(三)--高级装配
一.Spring profile 在开发软件的时候,有一个很大的挑战就是将应用程序从一个环境迁 移到另外一个环境.开发阶段中,某些环境相关做法可能并不适合迁 移到生产环境中,甚至即便迁移过去也无法正常 ...
- Spring学习笔记--自动装配Bean属性
Spring提供了四种类型的自动装配策略: byName – 把与Bean的属性具有相同名字(或者ID)的其他Bean自动装配到Bean的对应属性中. byType – 把与Bean的属性具有相同类型 ...
- Spring学习(三)-----Spring自动装配Beans
在Spring框架,可以用 auto-wiring 功能会自动装配Bean.要启用它,只需要在 <bean>定义“autowire”属性. <bean id="custom ...
- Spring(六)之自动装配
一.自动装配模型 下面是自动连接模式,可以用来指示Spring容器使用自动连接进行依赖注入.您可以使用元素的autowire属性为bean定义指定autowire模式. 可以使用 byType 或者 ...
- Spring 由构造函数自动装配
Spring 由构造函数自动装配,这种模式与 byType 非常相似,但它应用于构造器参数. Spring 容器看作 beans,在 XML 配置文件中 beans 的 autowire 属性设置为 ...
- 【面试普通人VS高手系列】Spring Boot中自动装配机制的原理
最近一个粉丝说,他面试了4个公司,有三个公司问他:"Spring Boot 中自动装配机制的原理" 他回答了,感觉没回答错误,但是怎么就没给offer呢? 对于这个问题,看看普通人 ...
- spring学习总结——高级装配学习二(处理自动装配的歧义性)
我们已经看到如何使用自动装配让Spring完全负责将bean引用注入到构造参数和属性中.自动装配能够提供很大的帮助.不过,spring容器中仅有一个bean匹配所需的结果时,自动装配才是有效的.如果不 ...
- Spring 学习——Spring注解——Autowiring(自动装配)
装配方式 方式一:默认 方式二:byName:根据属性名称自动装配.会查找Bean容器内部所有初始化的与属性名成相同的Bean,自动装配.(需要通过set方法注入,注入Bean的id名称需要和实体类的 ...
- Spring注解驱动开发(三)-----自动装配
自动装配 概念 Spring利用依赖注入(DI),完成对IOC容器中中各个组件的依赖关系赋值. @Autowired-----自动注入 1.默认优先按照类型去容器中找对应的组件 application ...
随机推荐
- 对DataSet,DataRow,DateTable转换成相应的模型
/// <summary> /// DataRow 转成 模型 /// </summary> /// <t ...
- Redis,传统数据库,HBase,Hive区别联系
首先介绍各个数据库: Redis: 传统数据库: HBase: Hive:
- C++编译错误杂记
目录 2018年12月23日 error: no matching function for call to ××× 2018年12月10日 error: expected ')' before '* ...
- 嵌入式框架Zorb Framework搭建一:嵌入式环境搭建、调试输出和建立时间系统
我是卓波,我是一名嵌入式工程师,我万万没想到我会在这里跟大家吹牛皮. 嵌入式框架Zorb Framework搭建过程 嵌入式框架Zorb Framework搭建一:嵌入式环境搭建.调试输出和建立时间系 ...
- Java8 Lambda表达式实战之方法引用(一)
方法的引用 方法引用是用来直接访问类或者实例的已经存在的方法或者构造方法,方法引用提供了一种引用而不执行方法的方式,如果抽象方法的实现恰好可以使用调用另外一个方法来实现,就有可能可以使用方法引用 方法 ...
- 理解Python的装饰器
看Flask文档时候看到关于cache的装饰器,有这么一段代码: def cached(timeout=5 * 60, key=’view/%s’): def decorator(f): @wraps ...
- 使用nmon监控得出网络实时速度以及最大、最小、平均网络传送速度
首先我们得搞清楚几个概念,即什么是网速?什么是带宽? 举两个个例子: 1.家里装网线,宽带提供商说我们的带宽是100兆. 2.用迅雷下载电影,迅雷显示实时的下载速度是每秒3兆,或者说是3MB/s. 这 ...
- 「日常训练」Regular Bridge(Codeforces Round 306 Div.2 D)
题意与分析 图论基础+思维题. 代码 #include <bits/stdc++.h> #define MP make_pair #define PB emplace_back #defi ...
- hdu1422重温世界杯(动态规划,最长子序列)
重温世界杯 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- OSG-OSG中的observer_ptr指针
看array大神的CookBook后一些感想,在代码上添加了一些注释,也对源码做了一些研读,记录下学习的过程. CookBook中第一个例子就是observer_ptr指针,这个指针和它的名字一样,就 ...