`
mywebcode
  • 浏览: 1004360 次
文章分类
社区版块
存档分类
最新评论

spring入门(8)--装配Bean之自动装配

 
阅读更多

Spring_Autowiringcollaborators

Spring3.2.2中自动装配类型,分别为:no(default)(不采用自动装配)byNamebyTypeconstructor下面来分别介绍一下这些是如何自动装配的

<beanid="foo"class="...Foo"autowire="autowiretype">

Mode Explanation

no:(Default)Noautowiring.Beanreferencesmustbedefinedviaarefelement.

Changingthedefaultsettingisnotrecommendedforlargerdeployments,

becausespecifyingcollaboratorsexplicitlygivesgreatercontrolandclarity.

Tosomeextent,itdocumentsthestructureofasystem.

byName:Autowiringbypropertyname.

Springlooksforabeanwiththesamenameasthepropertythatneedstobeautowired.

Forexample,ifabeandefinitionissettoautowirebyname,

anditcontainsamasterproperty(thatis,ithasasetMaster(..)method),

Springlooksforabeandefinitionnamedmaster,andusesittosettheproperty.

byType:Allowsapropertytobeautowiredifexactlyonebeanofthepropertytypeexistsinthecontainer.

Ifmorethanoneexists,afatalexceptionisthrown,

whichindicatesthatyoumaynotusebyTypeautowiringforthatbean.

Iftherearenomatchingbeans,nothinghappens;thepropertyisnotset.

constructor:AnalogoustobyType,butappliestoconstructorarguments.

Ifthereisnotexactlyonebeanoftheconstructorargumenttypeinthecontainer,afatalerrorisraised

案例分析:

1、创建CumputerBean

  1. packagewww.csdn.spring.autowire.bean;
  2. publicclassCumputerBean{
  3. //电脑名称
  4. privateStringname;
  5. publicvoidsetName(Stringname){
  6. this.name=name;
  7. }
  8. }

2、创建DeptBean

  1. packagewww.csdn.spring.autowire.bean;
  2. publicclassDeptBean{
  3. //部门名称
  4. privateStringname;
  5. publicvoidsetName(Stringname){
  6. this.name=name;
  7. }
  8. }

3、创建EmployeeBean

  1. packagewww.csdn.spring.autowire.bean;
  2. publicclassEmployeeBean{
  3. privateDeptBeandeptBean;
  4. privateCumputerBeancumputerBean;
  5. publicvoidsetDeptBean(DeptBeandeptBean){
  6. this.deptBean=deptBean;
  7. }
  8. publicvoidsetCumputerBean(CumputerBeancumputerBean){
  9. this.cumputerBean=cumputerBean;
  10. }
  11. @Override
  12. publicStringtoString(){
  13. return"EmployeeBean[deptBean="+deptBean+",cumputerBean="
  14. +cumputerBean+"]";
  15. }
  16. }

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

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

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

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

  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beansxmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <!--电脑bean-->
  7. <beanid="cumputerBean"class="www.csdn.spring.autowire.bean.CumputerBean">
  8. <propertyname="name"value="HP6325笔记本"/>
  9. </bean>
  10. <!--部门bean-->
  11. <beanid="deptBean"class="www.csdn.spring.autowire.bean.DeptBean">
  12. <propertyname="name"value="CSDN教育事业部"/>
  13. </bean>
  14. <!--员工bean-->
  15. <beanid="employeeBean"class="www.csdn.spring.autowire.bean.EmployeeBean"autowire="byName"/>
  16. </beans>

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

  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beansxmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <!--电脑bean-->
  7. <beanid="cumputerBean"class="www.csdn.spring.autowire.bean.CumputerBean">
  8. <propertyname="name"value="HP6325笔记本"/>
  9. </bean>
  10. <!--部门bean-->
  11. <beanid="deptBean"class="www.csdn.spring.autowire.bean.DeptBean">
  12. <propertyname="name"value="CSDN教育事业部"/>
  13. </bean>
  14. <!--员工bean根据EmployeeBean中的属性名称去匹配-->
  15. <beanid="employeeBean"class="www.csdn.spring.autowire.bean.EmployeeBean"autowire="byType"/>
  16. </beans>

注意:当根据byType类型装配时,当在容器内找到多个匹配的类型时会出现如下bug

org.springframework.beans.factory.UnsatisfiedDependencyException:

Errorcreatingbeanwithname'employeeBean'definedinclasspathresource[spring-byType.xml]:Unsatisfieddependencyexpressedthroughbeanproperty'deptBean'::

Noqualifyingbeanoftype[www.csdn.spring.autowire.bean.DeptBean]isdefined:expectedsinglematchingbeanbutfound2:deptBean,deptBean1;

nestedexceptionisorg.springframework.beans.factory.NoUniqueBeanDefinitionException:

Noqualifyingbeanoftype[www.csdn.spring.autowire.bean.DeptBean]isdefined:expectedsinglematchingbeanbutfound2:deptBean,deptBean1

4、Constructor(构造器参数根据byType类型匹配,自动装配)

首先修改EmployeeBean类修改后代码如下:

  1. packagewww.csdn.spring.autowire.bean;
  2. publicclassEmployeeBean{
  3. privateDeptBeandeptBean;
  4. privateCumputerBeancumputerBean;
  5. publicEmployeeBean(DeptBeandeptBean,CumputerBeancumputerBean){
  6. super();
  7. this.deptBean=deptBean;
  8. this.cumputerBean=cumputerBean;
  9. }
  10. @Override
  11. publicStringtoString(){
  12. return"EmployeeBean[deptBean="+deptBean+",cumputerBean="
  13. +cumputerBean+"]";
  14. }
  15. }

配置文件操作:

  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beansxmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <!--电脑bean-->
  7. <beanid="cumputerBean"class="www.csdn.spring.autowire.bean.CumputerBean">
  8. <propertyname="name"value="HP6325笔记本"/>
  9. </bean>
  10. <!--部门bean-->
  11. <beanid="deptBean"class="www.csdn.spring.autowire.bean.DeptBean">
  12. <propertyname="name"value="CSDN教育事业部"/>
  13. </bean>
  14. <!--员工bean根据EmployeeBean中的属性名称bytype去匹配-->
  15. <beanid="employeeBean"class="www.csdn.spring.autowire.bean.EmployeeBean"
  16. autowire="constructor">
  17. </bean>
  18. </beans>

说明:

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

比如:

配置文件中只配置了CumpterBean

  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beansxmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <!--电脑bean-->
  7. <beanid="cumputerBean"class="www.csdn.spring.autowire.bean.CumputerBean">
  8. <propertyname="name"value="HP6325笔记本"/>
  9. </bean>
  10. <!--员工bean根据EmployeeBean中的属性名称bytype去匹配-->
  11. <beanid="employeeBean"class="www.csdn.spring.autowire.bean.EmployeeBean"
  12. autowire="constructor">
  13. </bean>
  14. </beans>

会出现如下bug:

org.springframework.beans.factory.UnsatisfiedDependencyException:

Errorcreatingbeanwithname'employeeBean'definedinclasspathresource[spring-constructors.xml]:Unsatisfieddependencyexpressedthroughconstructorargumentwithindex0oftype[www.csdn.spring.autowire.bean.DeptBean]::

Noqualifyingbeanoftype[www.csdn.spring.autowire.bean.DeptBean]foundfordependency:

expectedatleast1beanwhichqualifiesasautowirecandidateforthisdependency.Dependencyannotations:{};

nestedexceptionisorg.springframework.beans.factory.NoSuchBeanDefinitionException:

Noqualifyingbeanoftype[www.csdn.spring.autowire.bean.DeptBean]foundfordependency:

expectedatleast1beanwhichqualifiesasautowirecandidateforthisdependency.Dependencyannotations:{}

Causedby:

org.springframework.beans.factory.NoSuchBeanDefinitionException:

Noqualifyingbeanoftype[www.csdn.spring.autowire.bean.DeptBean]foundfordependency:

expectedatleast1beanwhichqualifiesasautowirecandidateforthisdependency.Dependencyannotations:{}

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

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

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

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

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

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

  1. org.springframework.beans.factory.UnsatisfiedDependencyException:
  2. Errorcreatingbeanwithname'employeeBean'definedinclasspathresource[spring-constructors.xml]:
  1. Unsatisfieddependencyexpressedthroughconstructorargumentwithindex0oftype[www.csdn.spring.autowire.bean.DeptBean]::
  2. Noqualifyingbeanoftype[www.csdn.spring.autowire.bean.DeptBean]isdefined:
  3. expectedsinglematchingbeanbutfound2:deptBean1,deptBean2;
  4. nestedexceptionisorg.springframework.beans.factory.NoUniqueBeanDefinitionException:
  5. Noqualifyingbeanoftype[www.csdn.spring.autowire.bean.DeptBean]isdefined:
  1. expectedsinglematchingbeanbutfound2:deptBean1,deptBean2
分享到:
评论

相关推荐

    spring入门学习-3、Bean装配(XML).pdf

    spring入门学习-3、Bean装配(XML).pdf

    详解Spring框架---IOC装配Bean

    本篇文章主要介绍了详解Spring框架---IOC装配Bean,提供了三种方式实例化Bean,具有一定的参考价值,有兴趣的可以了解一下。

    基于框架的Web开发-装配Bean自动装配.doc

    1.4 装配Bean-自动装配(重要!) Spring从两个角度来实现自动化装配: 组件扫描(component scanning):Spring会自动发现应用上下文中所创建的bean。 自动装配(autowiring):Spring自动满足bean之间的依赖。 1 ...

    spring自动装配例子

    ean的自动装配,有4种 (1)no:不做任何操作 (2)byName:根据属性 名 自动装配,设值注入 &lt;bean id="xxx" class="xxx" &gt;&lt;/bean&gt; (3)byType:根据属性 类型 自动装配,相同类型多个会抛出异常,设值注入 &lt;bean...

    spring-framework-5.0.4.RELEASEspring-framework-5.0.4.RELEASE

    Spring是什么呢?首先它是一个开源的项目,而且目前非常活跃...它实现了很优雅的MVC,对不同的数据访问技术提供了统一的接口,采用IOC使得可以很容易的实现bean的装配,提供了简洁的AOP并据此实现Transaction Management

    spring-aop-4.0.0.RELEASE

    spring-aop-4.0.0.RELEASE工具类,用于spring注解装配bean

    spring入门教程之bean的继承与自动装配详解

    众所周知Spring里面的bean就类似是定义的一个组件,而这个组件的作用就是实现某个功能的,下面这篇文章主要给大家介绍了关于spring入门教程之bean继承与自动装配的相关资料,需要的朋友可以参考借鉴,下面随着小编来...

    spring装配bean实例代码

    博客地址:https://blog.csdn.net/u010476739/article/details/76732201 spring装配bean的方式实例

    Spring基于@Conditional条件化装配bean

    主要介绍了Spring @Conditional条件化装配bean,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    day38 16-Spring的Bean的装配:注解的方式

    NULL 博文链接:https://364232252.iteye.com/blog/2369853

    spring-beans.zip

    它们被Spring IOC容器初始化,装配,和管理。这些beans通过容器中配置的元数据创建。比如,以XML文件中&lt;bean/&gt; 的形式定义。Spring 框架定义的beans都是单件beans。在bean tag中有个属性”singleton”,如果它被赋为...

    Spring中的Bean的管理_Bean的装配方式_基于注解的装配_项目

    目的:Spring容器已经成功获取了UserController实例,并通过调用实例中的方法执行了各层中的输出语句。 运行结果为: User [id=1, name=张三, password=123] userDao say hello world! UserService say hello world ...

    完整Spring框架,包含源码文档等 spring-5.2.9.RELEASE-dist.zip

    springframework 是sping 里面的一个开源框架...它实现了很优雅的MVC,对不同的数据访问技术提供了统一的接口,采用IOC使得可以很容易的实现bean的装配,提供了简洁的AOP并据此实现Transaction Management,等等......

    springIOC核心组件分析.vsdx

    spring-beans:Bean工厂与装配 spring-context:上下文,即IOC容器 spring-context-support:对IOC的扩展,以及IOC子容器 spring-context-indexer:类管理组件和Classpath扫描 spring-expression:表达式语句 切面编程: ...

    浅谈Spring装配Bean之组件扫描和自动装配

    本篇文章主要介绍了浅谈Spring装配Bean之组件扫描和自动装配,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    spring装配bean的3种方式总结

    主要给大家介绍了关于spring装配bean的3种方式,文中通过示例代码介绍的非常详细,对大家的学习或者使用Spring具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧

    Spring自动装配Bean实现过程详解

    主要介绍了Spring自动装配Bean实现过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    spring在IoC容器中装配Bean详解

    主要介绍了spring在IoC容器中装配Bean详解,具有一定借鉴价值,需要的朋友可以参考下

    Spring Boot技术知识点:Bean装配1

    Spring Boot技术知识点:Bean装配1

    JSP Spring 自动化装配Bean实例详解

    主要介绍了JSP Spring 自动化装配Bean实例详解的相关资料,需要的朋友可以参考下

Global site tag (gtag.js) - Google Analytics