Spring的自动装配Bean的三种方式

 更新时间:2017年02月27日 12:00:01   作者:漏断人初静v  
本篇文章主要介绍了 Spring的自动装配Bean的三种方式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

spring的自动装配功能的定义:无须在Spring配置文件中描述javaBean之间的依赖关系(如配置<property>、<constructor-arg>)。IOC容器会自动建立javabean之间的关联关系。

如果没有采用自动装配的话,手动装配我们通常在配置文件中进行实现:一下代码就是手动装配:

<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-3.0.xsd"> 
 
  <bean id="customerDAO" class="com.hebeu.customer.dao.JdbcCustomerDAO"> 
    <property name="dataSource" ref="dataSource" /> 
  </bean> 
 
</beans> 

通过<property name="dataSource" ref="dataSource" />向customerDAO的bean中注入了dataSource

在Spring框架,可以用 auto-wiring 功能会自动装配Bean。要启用它,只需要在 <bean>定义“autowire”属性。

<bean id="customer" class="com.yiibai.common.Customer" autowire="byName" />

在Spring中,支持 5 自动装配模式。

  • no – 缺省情况下,自动配置是通过“ref”属性手动设定
  • byName – 根据属性名称自动装配。如果一个bean的名称和其他bean属性的名称是一样的,将会自装配它。
  • byType – 按数据类型自动装配。如果一个bean的数据类型是用其它bean属性的数据类型,兼容并自动装配它。
  • constructor – 在构造函数参数的byType方式。
  • autodetect – 如果找到默认的构造函数,使用“自动装配用构造”; 否则,使用“按类型自动装配”。【在Spring3.0以后的版本被废弃,已经不再合法了】

第一种自动装配【根据属性名称自动装配】

package com.hebeu.model; 
 
public class Customer { 
 
  private Address address; 
   
  public Customer() { 
     
  } 
   
  public Customer(int id, Address address) { 
    super(); 
    this.address = address; 
  } 
 
  public Address getAddress() { 
    return address; 
  } 
 
  public void setAddress(Address address) { 
    this.address = address; 
  } 
   
} 

package com.hebeu.model; 
 
public class Address { 
 
  private String fulladdress; 
   
  public Address(){ 
     
  } 
   
  public Address(String addr){ 
    this.fulladdress = addr; 
  } 
 
  public String getFulladdress() { 
    return fulladdress; 
  } 
 
  public void setFulladdress(String fulladdress) { 
    this.fulladdress = fulladdress; 
  } 
   
} 

<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-3.0.xsd"> 
   
  <bean id="customer" class="com.hebeu.model.Customer" autowire="byName"></bean>
   
  <bean id="address" class="com.hebeu.model.Address"> 
    <property name="fulladdress" value="YiLong Road, CA 188"></property> 
  </bean> 
   
</beans>  

这样就将address注入到Customer中。这就是自动注入ByName.在Customer bean中公开了一个属性address,Spring容器会找到address bean,并且装配。这里必须要注意,Customer中要被注入的bean的set方法要求必须是public的,否则会报空指针异常。还有配置的bean的id必须和Customer中声明的变量名相同。

第二种自动装配【根据数据类型自动装配】

声明的俩个bean同样为Customer以及Address,将applicationContext.xml转换为这样的就是实现根据数据类型进行自动装配。

<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-3.0.xsd"> 
   
  <bean id="customer" class="com.hebeu.model.Customer" <strong><span style="color:#FF0000;">autowire="byType"</span></strong>></bean> 
   
  <bean id="bean1" class="com.hebeu.model.Address"> 
    <property name="fulladdress" value="YiLong Road, CA 188"></property> 
  </bean> 
</beans>  

类型自动装配的意思是如果一个bean的数据类型与其他的bean属性的数据类型相同,将会自动兼容装配它。当然要求只能配置一个某一个类型的bean.如果配置成这样,那么是会出错的。

<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-3.0.xsd"> 
   
  <bean id="customer" class="com.hebeu.model.Customer" autowire="byType"></bean> 
   
  <bean id="bean1" class="com.hebeu.model.Address"> 
    <property name="fulladdress" value="YiLong Road, CA 188"></property> 
  </bean> 
  <bean id="bean2" class="com.hebeu.model.Address"> 
    <property name="fulladdress" value="YiLong Road, CA 188"></property> 
  </bean> 
</beans>  

第三种自动装配【根据构造方法自动装配】

案例同上,将applicationContext.xml转换为如下,就实现了按照构造方法自动装配:

<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-3.0.xsd"> 
   
  <bean id="customer" class="com.hebeu.model.Customer"> 
    <!-- 构造方法的参数 --> 
    <constructor-arg> 
      <ref bean="bean1"/> 
    </constructor-arg> 
  </bean> 
   
  <bean id="bean1" class="com.hebeu.model.Address"> 
    <property name="fulladdress" value="YiLong Road, CA 188"></property> 
  </bean> 
   
</beans> 

 它实际上是构造函数的参数类型自动装配。这意味着如果一个bean的数据类型与其他bean的构造器参数的数据类型是相同的,那么就自动装配。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Java 中HttpURLConnection附件上传的实例详解

    Java 中HttpURLConnection附件上传的实例详解

    这篇文章主要介绍了Java 中HttpURLConnection附件上传的实例详解的相关资料,希望通过本文大家能掌握这样的知识内容,需要的朋友可以参考下
    2017-09-09
  • 谈谈Spring 注入properties文件总结

    谈谈Spring 注入properties文件总结

    本篇谈谈Spring 注入properties文件总结,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-01-01
  • Java开启线程的四种方法案例详解

    Java开启线程的四种方法案例详解

    这篇文章主要介绍了Java开启线程的四种方法,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-02-02
  • Java List的remove()方法踩坑

    Java List的remove()方法踩坑

    Java的List在删除元素时,一般会用list.remove(o)/remove(i)方法。在使用时,容易触碰陷阱,本文就来介绍一下容易踩的坑,感兴趣的可以了解一下
    2021-10-10
  • springboot2.5.0和redis整合配置详解

    springboot2.5.0和redis整合配置详解

    本篇文章向大家介绍springboot2.5.0 整合 redis 配置方法,教大家在pom添加依赖的方法如何调用redis,本文通过实例代码给大家介绍的非常详细,需要的朋友可以参考下
    2021-06-06
  • springboot ehcache 配置使用方法代码详解

    springboot ehcache 配置使用方法代码详解

    EhCache是一个比较成熟的Java缓存框架,Springboot对ehcache的使用非常支持,所以在Springboot中只需做些配置就可使用,且使用方式也简易,今天给大家分享springboot ehcache 配置使用教程,一起看看吧
    2021-06-06
  • Java中CyclicBarrier的理解与应用详解

    Java中CyclicBarrier的理解与应用详解

    这篇文章主要介绍了Java中CyclicBarrier的理解与应用详解,CyclicBarrier类是JUC框架中的工具类,也是一个同步辅助装置:允许多个线程去等待直到全部线程抵达了公共的栅栏点,需要的朋友可以参考下
    2023-12-12
  • Springboot指定扫描路径的实现示例

    Springboot指定扫描路径的实现示例

    本文主要介绍了Springboot指定扫描路径的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-05-05
  • 基于Java编写简单的Excel工具类

    基于Java编写简单的Excel工具类

    这篇文章主要为大家详细介绍了如何基于Java编写简单的Excel工具类,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以参考下
    2024-02-02
  • Java 内部类的定义与范例

    Java 内部类的定义与范例

    说起内部类这个词,想必很多人都不陌生,但是又会觉得不熟悉。原因是平时编写代码时可能用到的场景不多,用得最多的是在有事件监听的情况下,并且即使用到也很少去总结内部类的用法。今天我们就来一探究竟
    2021-11-11

最新评论