`
javawangli
  • 浏览: 221217 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Spring 中Bean的自动装配六种模式,你懂得几种?

阅读更多

       Spring Bean的自动装配六种模式,你懂得几种?

  

       Spring IoC容器可以自动装配(autowire)相互协作bean之间的关联关系。因此,如果可能的话,可以自动让Spring通过检查BeanFactory中的内容,来替我们指定bean的协作者(其他被依赖的bean)。autowire一共有种类型。由于autowire可以针对单个bean进行设置,因此可以让有些bean使用autowire,有些bean不采用。autowire的方便之处在减少或者消除属性或构造器参数的设置,这样可以给我们的配置文件减减肥![2xml配置文件中,可以在<bean/>元素中使用autowire属性指定:

 

 

 

模式

说明

  Default

在每个bean中都一个autowire=default的默认配置它的含义是:

采用beans和跟标签中的default-autowire="属性值"一样的设置。

 

  On

不使用自动装配,必须通过ref元素指定依赖,默认设置。

 

  ByNname

根据属性名自动装配。此选项将检查容器并根据名字查找与属性完全一致的bean,并将其与属性自动装配。例如,在bean定义中将autowire设置为by name,而该bean包含master属性(同时提供setMaster(..)方法),Spring就会查找名为masterbean定义,并用它来装配给master属性。

  Bytype

如果容器中存在一个与指定属性类型相同的bean,那么将与该属性自动装配。如果存在多个该类型的bean,那么将会抛出异常,并指出不能使用byType方式进行自动装配。若没有找到相匹配的bean,则什么事都不发生,属性也不会被设置。如果你不希望这样,那么可以通过设置dependency-check="objects"Spring抛出异常。

 

 Constructor

byType的方式类似,不同之处在于它应用于构造器参数。如果在容器中没有找到与构造器参数类型一致的bean,那么将会抛出异常。 

Antodetect

通过bean类的自省机制(introspection)来决定是使用constructor还是byType方式进行自动装配。如果发现默认的构造器,那么将使用byType方式

 

 

 

 

 

 

 

下来我们就用案例来证明一下:准备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;

}

}

 

1defaultnodefault.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"

default-autowire="no">

<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="default" />

</beans>

 

 

 

 

 

 测试类:(junit测试)  

 

 

 public class App { 

@Test

public void test(){

ApplicationContext ac= new ClassPathXmlApplicationContext("classpath:default.xml"); EmpServiceImpl emp = (EmpServiceImpl) ac.getBean("empServiceImpl");

}

}

 

 声明:

<bean id="empServiceImpl" class="cn.csdn.service.EmpServiceImpl"

scope="singleton" autowire="no" />

不使用自动装配,必须通过ref元素指定依赖,默认设置。

 

  

 

 

 

2,byName值的byname.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="byName" />

</beans>

 

 

 

 测试类:(junit测试)  

 

 public class App { 

@Test

public void test(){

ApplicationContext ac=new ClassPathXmlApplicationContext("classpath:byName.xml"); EmpServiceImpl emp = (EmpServiceImpl) ac.getBean("empServiceImpl");

}

}

 

 

3,byTypebytype.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");

}

}

 

注意:

   byName byType

   在使用的过程中必须保证bean能够初始化,否则的话会出现bug

   如果有默认的无参数的构造器就不需要多余的配置

   如果有带有参数的构造器,那在bean的配置中必须配置器初始化的参数 或者在bean中添加无参数的构造器

 

 

4, Constructor值的constructor.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  相同类型只能在 配置文件中出现一次

<bean id="homeAddressServiceImpl" class="cn.csdn.service.HomeAddressServiceImpl" scope="prototype">

  <property name="address">

    <value>北京</value>

  </property>

</bean>

<!-- 自动装配 采用constructor 构造器中的参数是按照byType进行装配的

<bean id="empServiceImpl" class="cn.csdn.service.EmpServiceImpl" scope="singleton" autowire="constructor"/>

</beans>

 

 

 测试类:(junit测试)  

     

 public class App { 

@Test

public void test(){

ApplicationContext ac=new ClassPathXmlApplicationContext("classpath:byName.xml"); EmpServiceImpl emp = (EmpServiceImpl) ac.getBean("empServiceImpl");

}

}

 

5Antodetect值的antodetect.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 -->

<bean id="addressServiceImpl" class="cn.csdn.service.AddressServiceImpl" scope="singleton">

  <property name="address">

    <value>北京</value>

  </property>

</bean>

<bean id="empServiceImpl" class="cn.csdn.service.EmpServiceImpl" scope="singleton" autowire="autodetect"/>

</beans>

 

测试类:(junit测试)  

 

 public class App { 

@Test

public void test(){

ApplicationContext ac=new ClassPathXmlApplicationContext("classpath:byName.xml"); EmpServiceImpl emp = (EmpServiceImpl) ac.getBean("empServiceImpl");

}

}

 

 

 

  

 

  结束语:  

 大部分学着都认为Bean的自动装配有5种模式,但详细的说是六种模式,他们往往忽略了default 值的应用,在每个bean中都一个autowire=default的默认配置它的含义是:采用beans和跟标签中的default-autowire="属性值"一样的设置,这个值是不能忽略的!

*可以设置bean使自动装配失效:
采用xml格式配置bean时,将<bean/>元素的autowire-candidate属性设置为false,这样容器在查找自动装配对象时,将不考虑该bean,即它不会被考虑作为其它bean自动装配的候选者,但是该bean本身还是可以使用自动装配来注入其它bean的。

 

<!--EndFragment-->

5
7
分享到:
评论
2 楼 dare_ 2014-09-23  
官网的文档明确说明 是五种 你的默认和no应该是同一种
1 楼 javawangli 2011-04-21  
[color=darkred][/color] 是不是大部分人认为有5种?但你你们忽略的往往是最重要的

相关推荐

    spring装配bean的3种方式总结

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

    java面试Spring.pdf

    1. Spring 介绍 1.1 Spring 的优点 ...说一下Spring基于xml注入bean的几种方式? Spring如何解决循环依赖问题? Spring的自动装配 Spring框架中都用到了哪些设计模式? Spring框架中有哪些不同类型的事件?

    Spring面试专题.pdf

    6、Spring 有几种配置方式? 7、如何用基于 XML 配置的方式配置 Spring? 8、如何用基于 Java 配置的方式配置 Spring? 9、怎样用注解的方式配置 Spring? 10、请解释 Spring Bean 的生命周期? 11、Spring Bean 的...

    Spring面试题.zip

    6、Spring 有几种配置方式? 7、如何用基于 XML 配置的方式配置 Spring? 8、如何用基于 Java 配置的方式配置 Spring? 9、怎样用注解的方式配置 Spring? 10、请解释 Spring Bean 的生命周期? 11、Spring Bean 的...

    Spring学习之Bean的装配多种方法

    本篇文章主要介绍了Spring学习之Bean的装配三种方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    25个经典的Spring面试问答

    Spring有几种配置方式 如何用基于XML配置的方式配置Spring 如何用基于Java配置的方式配置Spring 怎样用注解的方式配置Spring 请解释Spring Bean的生命周期 Spring Bean的作用域之间有什么区别 什么是Spring inner ...

    Spring面试题含答案.pdf

    25. 解释 Spring 支持的几种 bean 的作用域 26. Spring 框架中的单例 bean 是线程安全的吗? 27. 解释 Spring 框架中 bean 的生命周期 28. 哪些是重要的 bean 生命周期方法? 你能重载它们吗? 29. 什么是 Spring ...

    Spring Bean常用的的装配方式详解

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

    吴天雄--Spring笔记.doc

    IOC详解,Spring环境搭建,Spring创建Bean的三种方式,scope属性详解(包含单例设计模式),DI详解,Spring的几种注入方式,利用Spring简化Mybatis;第二天内容:AOP(AOP常用概念、Spring的三种aop实现方式、代理...

    高级开发spring面试题和答案.pdf

    传播特性有几种?7种; 某一个事务嵌套另一个事务的时候怎么办? REQUIRED_NEW和REQUIRED区别 Spring的事务是如何回滚的,实现原理; 抽象类和接口的区别,什么时候用抽象类什么时候用接口; StringBuilder和...

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

    2.4.1 四种自动装配类型 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装配 ...

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

    2.4.1 四种自动装配类型 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装配 ...

    Spring in Action(第2版)中文版

    2.4.1四种自动装配类型 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声明父...

    Java常见面试题208道.docx

    15.java 中 IO 流分为几种? 16.BIO、NIO、AIO 有什么区别? 17.Files的常用方法都有哪些? 二、容器 18.java 容器都有哪些? 19.Collection 和 Collections 有什么区别? 20.List、Set、Map 之间的区别是什么? 21....

    史上最全java面试,103项重点知识,带目录

    15. java 中 IO 流分为几种? 7 16. BIO、NIO、AIO 有什么区别? 7 17. Files的常用方法都有哪些? 8 二、容器 8 18. java 容器都有哪些? 8 19. Collection 和 Collections 有什么区别? 9 20. List、Set、Map 之间...

Global site tag (gtag.js) - Google Analytics