`
endual
  • 浏览: 3509742 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Spring3.2.2_自动装配

 
阅读更多

Spring3.2.2_自动装配

分类: Spring课程总结 157人阅读 评论(0) 收藏 举报

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

[java] view plaincopy
 
  1. package www.csdn.spring.autowire.bean;  
  2.   
  3. public class CumputerBean {  
  4.   
  5. // 电脑名称  
  6.   
  7. private String name;  
  8.   
  9. public void setName(String name) {  
  10.   
  11. this.name = name;  
  12.   
  13. }  
  14.   
  15. }  

2、创建DeptBean 

[java] view plaincopy
 
  1. package www.csdn.spring.autowire.bean;  
  2.   
  3.   
  4. public class DeptBean {  
  5.   
  6. //部门名称  
  7.   
  8. private String name;  
  9.   
  10. public void setName(String name) {  
  11.   
  12. this.name = name;  
  13.   
  14. }  
  15.   
  16. }  

3、创建EmployeeBean

[java] view plaincopy
 
  1. package www.csdn.spring.autowire.bean;  
  2.   
  3.   
  4. public class EmployeeBean {  
  5.   
  6. private DeptBean deptBean;  
  7.   
  8. private CumputerBean cumputerBean;  
  9.   
  10.   
  11. public void setDeptBean(DeptBean deptBean) {  
  12.   
  13. this.deptBean = deptBean;  
  14.   
  15. }  
  16.   
  17. public void setCumputerBean(CumputerBean cumputerBean) {  
  18.   
  19. this.cumputerBean = cumputerBean;  
  20.   
  21. }  
  22.   
  23. @Override  
  24.   
  25. public String toString() {  
  26.   
  27. return "EmployeeBean [deptBean=" + deptBean + ", cumputerBean="  
  28.   
  29. + cumputerBean + "]";  
  30.   
  31. }  
  32.   
  33.   
  34. }  

首先分析nobyNamebyType的配置都是采用setter方法依赖注入实现的案例

1、no配置(通过ref=””引用需要的bean

[java] view plaincopy
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.            http://www.springframework.org/schema/beans/spring-beans.xsd">  
  7.   
  8. <!-- 电脑bean -->  
  9. <bean id="cumputerBean" class="www.csdn.spring.autowire.bean.CumputerBean">  
  10. <property name="name" value="HP6325笔记本" />  
  11. </bean>  
  12.   
  13. <!-- 部门bean -->  
  14. <bean id="deptBean" class="www.csdn.spring.autowire.bean.DeptBean">  
  15. <property name="name" value="CSDN教育事业部" />  
  16. </bean>  
  17.   
  18. <!-- 员工bean  根据EmployeeBean中的属性名称通过ref="bean"去匹配-->  
  19. <bean id="employeeBean" class="www.csdn.spring.autowire.bean.EmployeeBean">  
  20. <property name="cumputerBean" ref="cumputerBean" />  
  21. <property name="deptBean" ref="deptBean" />  
  22.   
  23. </bean>  
  24.   
  25. </beans>  

2、byName配置(分析:会根据EmployeeBean中属性的名称 自动装配)

[java] view plaincopy
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.   
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6.   
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans  
  8.   
  9.            http://www.springframework.org/schema/beans/spring-beans.xsd">  
  10.   
  11.   
  12.   
  13. <!-- 电脑bean -->  
  14.   
  15. <bean id="cumputerBean" class="www.csdn.spring.autowire.bean.CumputerBean">  
  16.   
  17. <property name="name" value="HP6325笔记本" />  
  18.   
  19. </bean>  
  20.   
  21. <!-- 部门bean -->  
  22.   
  23. <bean id="deptBean" class="www.csdn.spring.autowire.bean.DeptBean">  
  24.   
  25. <property name="name" value="CSDN教育事业部" />  
  26.   
  27. </bean>  
  28.   
  29. <!-- 员工bean-->  
  30.   
  31. <bean id="employeeBean" class="www.csdn.spring.autowire.bean.EmployeeBean" autowire="byName"/>  
  32.   
  33. </beans>  
[java] view plaincopy
 
  1.    

3、byType配置(分析:会根据EmployeeBean中属性的类型 自动装配)

[java] view plaincopy
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.   
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6.   
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans  
  8.   
  9.            http://www.springframework.org/schema/beans/spring-beans.xsd">  
  10.   
  11. <!-- 电脑bean -->  
  12.   
  13. <bean id="cumputerBean" class="www.csdn.spring.autowire.bean.CumputerBean">  
  14.   
  15. <property name="name" value="HP6325笔记本" />  
  16.   
  17. </bean>  
  18.   
  19. <!-- 部门bean -->  
  20.   
  21. <bean id="deptBean" class="www.csdn.spring.autowire.bean.DeptBean">  
  22.   
  23. <property name="name" value="CSDN教育事业部" />  
  24.   
  25. </bean>  
  26.   
  27. <!-- 员工bean  根据EmployeeBean中的属性根据类型匹配-->  
  28.   
  29. <bean id="employeeBean" class="www.csdn.spring.autowire.bean.EmployeeBean" autowire="byType"/>  
  30.   
  31.   
  32. </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类 修改后代码如下:

[java] view plaincopy
 
  1. package www.csdn.spring.autowire.bean;  
  2.   
  3.   
  4. public class EmployeeBean {  
  5.   
  6. private DeptBean deptBean;  
  7. private CumputerBean cumputerBean;  
  8.   
  9.   
  10. public EmployeeBean(DeptBean deptBean, CumputerBean cumputerBean) {  
  11. super();  
  12. this.deptBean = deptBean;  
  13. this.cumputerBean = cumputerBean;  
  14. }  
  15.   
  16. @Override  
  17. public String toString() {  
  18. return "EmployeeBean [deptBean=" + deptBean + ", cumputerBean="  
  19. + cumputerBean + "]";  
  20. }  
  21. }  
[java] view plaincopy
 
  1.    

配置文件操作:

[html] view plaincopy
 
  1.   <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.   
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6.   
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans  
  8.   
  9.            http://www.springframework.org/schema/beans/spring-beans.xsd">  
  10.   
  11. <!-- 电脑bean -->  
  12.   
  13. <bean id="cumputerBean" class="www.csdn.spring.autowire.bean.CumputerBean">  
  14. <property name="name" value="HP6325笔记本" />  
  15. </bean>  
  16.   
  17. <!-- 部门bean -->  
  18.   
  19. <bean id="deptBean" class="www.csdn.spring.autowire.bean.DeptBean">  
  20. <property name="name" value="CSDN教育事业部" />  
  21. </bean>  
  22.   
  23. <!-- 员工bean 根据EmployeeBean中的 属性名称 bytype 去匹配 -->  
  24. <bean id="employeeBean" class="www.csdn.spring.autowire.bean.EmployeeBean"  
  25. autowire="constructor">  
  26. </bean>  
  27.   
  28. </beans>  

说明:

1、当构造器的参数类型在容器中找不全时。

 比如:

   配置文件中只配置了CumpterBean

 

  

[html] view plaincopy
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.   
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6.   
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans  
  8.   
  9.            http://www.springframework.org/schema/beans/spring-beans.xsd">  
  10.   
  11. <!-- 电脑bean -->  
  12.   
  13. <bean id="cumputerBean" class="www.csdn.spring.autowire.bean.CumputerBean">  
  14.   
  15. <property name="name" value="HP6325笔记本" />  
  16.   
  17. </bean>  
  18.   
  19. <!-- 员工bean 根据EmployeeBean中的 属性名称 bytype 去匹配 -->  
  20.   
  21. <bean id="employeeBean" class="www.csdn.spring.autowire.bean.EmployeeBean"  
  22.   
  23. autowire="constructor">  
  24.   
  25. </bean>  
  26.   
  27. </beans>  
[html] view plaincopy
 
  1.    

会出现如下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、当配置文件找到构造器参数的类型有多个的时候比如配置文件如下:

[html] view plaincopy
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.   
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6.   
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans  
  8.   
  9.            http://www.springframework.org/schema/beans/spring-beans.xsd">  
  10.   
  11. <!-- 电脑bean -->  
  12.   
  13. <bean id="cumputerBean" class="www.csdn.spring.autowire.bean.CumputerBean">  
  14.   
  15. <property name="name" value="HP6325笔记本" />  
  16.   
  17. </bean>  
  18.   
  19. <!-- 部门bean -->  
  20.   
  21. <bean id="deptBean" class="www.csdn.spring.autowire.bean.DeptBean">  
  22.   
  23. <property name="name" value="CSDN教育事业部" />  
  24.   
  25. </bean>  
  26.   
  27. <!-- 部门bean -->  
  28.   
  29. <bean id="deptBean1" class="www.csdn.spring.autowire.bean.DeptBean">  
  30.   
  31. <property name="name" value="CSDN教育事业部" />  
  32.   
  33. </bean>  
  34.   
  35.   
  36. <!-- 员工bean 根据EmployeeBean中的 属性名称 bytype 去匹配 -->  
  37.   
  38. <bean id="employeeBean" class="www.csdn.spring.autowire.bean.EmployeeBean"  
  39.   
  40. autowire="constructor">  
  41.   
  42. </bean>  
  43.   
  44. </beans>  

 

说明:上面配置有两个同样类型的DeptBean但是不会出现bug,原因是在EmployeeBean中构造器接受的参数名称与deptBean一致。

3、当配置文件找到构造器参数的类型有多个的时候比如配置文件如下:

[html] view plaincopy
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.   
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6.   
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans  
  8.   
  9.            http://www.springframework.org/schema/beans/spring-beans.xsd">  
  10.   
  11. <!-- 电脑bean -->  
  12.   
  13. <bean id="cumputerBean" class="www.csdn.spring.autowire.bean.CumputerBean">  
  14.   
  15. <property name="name" value="HP6325笔记本" />  
  16.   
  17. </bean>  
  18.   
  19. <!-- 部门bean -->  
  20.   
  21. <bean id="deptBean1" class="www.csdn.spring.autowire.bean.DeptBean">  
  22.   
  23. <property name="name" value="CSDN教育事业部" />  
  24.   
  25. </bean>  
  26.   
  27. <!-- 部门bean -->  
  28.   
  29. <bean id="deptBean2" class="www.csdn.spring.autowire.bean.DeptBean">  
  30.   
  31. <property name="name" value="CSDN教育事业部" />  
  32.   
  33. </bean>  
  34.   
  35. <!-- 员工bean 根据EmployeeBean中的 属性名称 bytype 去匹配 -->  
  36.   
  37. <bean id="employeeBean" class="www.csdn.spring.autowire.bean.EmployeeBean"  
  38.   
  39. autowire="constructor">  
  40.   
  41. </bean>  
  42.   
  43. </beans>  

会出现如下bug(与byTypebug一致):

[java] view plaincopy
 
  1. org.springframework.beans.factory.UnsatisfiedDependencyException:   
  2.   
  3. Error creating bean with name 'employeeBean' defined in class path resource [spring-constructors.xml]:  
[java] view plaincopy
 
  1. Unsatisfied dependency expressed through constructor argument with index 0 of type [www.csdn.spring.autowire.bean.DeptBean]: :   
  2.   
  3. No qualifying bean of type [www.csdn.spring.autowire.bean.DeptBean] is defined:   
  4.   
  5. expected single matching bean but found 2: deptBean1,deptBean2;   
  6.   
  7. nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException:  
  8.   
  9.  No qualifying bean of type [www.csdn.spring.autowire.bean.DeptBean] is defined:   
[java] view plaincopy
 
  1. expected single matching bean but found 2: deptBean1,deptBean2  
分享到:
评论

相关推荐

    Spring-Reference_zh_CN(Spring中文参考手册)

    3.3.6. 自动装配(autowire)协作者 3.3.6.1. 设置Bean使自动装配失效 3.3.7. 依赖检查 3.3.8. 方法注入 3.3.8.1. Lookup方法注入 3.3.8.2. 自定义方法的替代方案 3.4. bean的作用域 3.4.1. Singleton作用域 3.4.2. ...

    spring in action英文版

     2.3.1 处理自动装配中的不确定性  2.3.2 混合使用自动和手动装配  2.3.3 缺省自动装配  2.3.4 何时采用自动装配  2.4 使用Spring的特殊Bean  2.4.1 对Bean进行后处理  2.4.2 对Bean工厂进行后...

    Spring in Action(第2版)中文版

    2.4.2混合使用自动和手动装配 2.4.3何时采用自动装配 2.5控制bean创建 2.5.1bean范围化 2.5.2利用工厂方法来创建bean 2.5.3初始化和销毁bean 2.6小结 第3章高级bean装配 3.1声明父bean和子bean 3.1.1抽象基...

    Spring in Action(第二版 中文高清版).part2

    2.4.2 混合使用自动和手动装配 2.4.3 何时采用自动装配 2.5 控制Bean创建 2.5.1 Bean范围化 2.5.2 利用工厂方法来创建Bean 2.5.3 初始化和销毁Bean 2.6 小结 第3章 高级Bean装配 3.1 声明父Bean和子Bean ...

    Spring in Action(第二版 中文高清版).part1

    2.4.2 混合使用自动和手动装配 2.4.3 何时采用自动装配 2.5 控制Bean创建 2.5.1 Bean范围化 2.5.2 利用工厂方法来创建Bean 2.5.3 初始化和销毁Bean 2.6 小结 第3章 高级Bean装配 3.1 声明父Bean和子Bean ...

    Spring.3.x企业应用开发实战(完整版).part2

    2.4.2 在Spring中装配Service 2.4.3 单元测试 2.5 展现层 2.5.1 配置Spring MVC框架 2.5.2 处理登录请求 2.5.3 JSP视图页面 2.6 运行Web应用 2.7 小结 第2篇 IoC和AOP 第3章 IoC容器概述 3.1 IoC概述 3.1.1 通过...

    Spring 2.0 开发参考手册

    3.3.6. 自动装配(autowire)协作者 3.3.7. 依赖检查 3.3.8. 方法注入 3.4. bean的作用域 3.4.1. Singleton作用域 3.4.2. Prototype作用域 3.4.3. 其他作用域 3.4.4. 自定义作用域 3.5. 定制bean特性 3.5.1...

    Spring中文帮助文档

    3.3.5. 自动装配(autowire)协作者 3.3.6. 依赖检查 3.3.7. 方法注入 3.4. Bean的作用域 3.4.1. Singleton作用域 3.4.2. Prototype作用域 3.4.3. Singleton beans和prototype-bean的依赖 3.4.4. 其他作用域 ...

    Spring API

    3.3.5. 自动装配(autowire)协作者 3.3.6. 依赖检查 3.3.7. 方法注入 3.4. Bean的作用域 3.4.1. Singleton作用域 3.4.2. Prototype作用域 3.4.3. Singleton beans和prototype-bean的依赖 3.4.4. 其他作用域 ...

    Spring攻略(第二版 中文高清版).part1

    1.12 用@Autowired和@Resource自动装配Bean 41 1.12.1 问题 41 1.12.2 解决方案 41 1.12.3 工作原理 41 1.13 继承Bean配置 47 1.13.1 问题 47 1.13.2 解决方案 47 1.13.3 工作原理 48 1.14 从...

    spring chm文档

    3.3.6. 自动装配(autowire)协作者 3.3.7. 依赖检查 3.3.8. 方法注入 3.4. bean的作用域 3.4.1. Singleton作用域 3.4.2. Prototype作用域 3.4.3. 其他作用域 3.4.4. 自定义作用域 3.5. 定制bean特性 3.5.1...

    Spring3.x企业应用开发实战(完整版) part1

    2.4.2 在Spring中装配Service 2.4.3 单元测试 2.5 展现层 2.5.1 配置Spring MVC框架 2.5.2 处理登录请求 2.5.3 JSP视图页面 2.6 运行Web应用 2.7 小结 第2篇 IoC和AOP 第3章 IoC容器概述 3.1 IoC概述 3.1.1 通过...

    Spring攻略(第二版 中文高清版).part2

    1.12 用@Autowired和@Resource自动装配Bean 41 1.12.1 问题 41 1.12.2 解决方案 41 1.12.3 工作原理 41 1.13 继承Bean配置 47 1.13.1 问题 47 1.13.2 解决方案 47 1.13.3 工作原理 48 1.14 从...

    JessMA Java Web 应用开发框架 (v3.2.2-20130815).pdf

    Dao Bean / Spring Bean装配、国际化、文件上传下载和缓存等基础Web应用组件,提供高度灵活的纯 Jsp/Servlet API 编程模型,完美整合 Spring,支持Action Convention“零配置”,能快速开发传统风格和RESTful风格的...

    java web技术开发大全(最全最新)

    JSP+Servlet+Struts+Hibernate+Spring+Ajax》内容包括Web客户端技术、JSP/Servlet技术、Struts 2(*、类型转换、输入校验、上传和下载文件、Struts 2的各种标签、对 AJAX的支持等)、Spring(Ioc容器、装配Java Bean...

    Java Web程序设计教程

    3.2.2jsp注释方式 37 3.2.3jsp声明方式 38 3.2.4jsp表达式的应用 39 3.2.5jsp的脚本段 39 3.2.6jsp的编译指令 40 3.2.7jsp的动作指令 41 3.2.8jsp的内置对象 43 3.3认识servlet 46 3.3.1servlet的开发 46 ...

    webx3框架指南PDF教程附学习Demo

    2.1. 用SpringExt装配服务 ............................................................................... 10 2.1.1. Spring Beans .........................................................................

    低清版 大型门户网站是这样炼成的.pdf

    2.5.2 调用校验框架进行自动校验 91 2.5.3 自定义国际化struts 2校验错误消息 92 2.5.4 struts 2的自带校验器 92 2.6 struts 2的拦截器 94 2.6.1 struts 2内建拦截器介绍 95 2.6.2 定义和使用拦截器栈 97 ...

    java web开发技术大全

    JSP+Servlet+Struts+Hibernate+Spring+Ajax》内容包括Web客户端技术、JSP/Servlet技术、Struts 2(*、类型转换、输入校验、上传和下载文件、Struts 2的各种标签、对 AJAX的支持等)、Spring(Ioc容器、装配Java Bean...

    《MyEclipse 6 Java 开发中文教程》前10章

    3.2.2工作区(Workspace) 56 3.2.3 导入、导出Java项目 56 3.2.3.1 导入项目 56 3.2.3.2 导出项目 57 3.2.4快速修正代码错误 57 3.2.5优化导入列表 58 3.2.6添加,修改,删除JRE 58 3.2.7查看类定义,层次和源码 58 ...

Global site tag (gtag.js) - Google Analytics