zoukankan      html  css  js  c++  java
  • 自动装配 Bean

    所谓自动装配,就是将一个 Bean 注入到其他 Bean 的 Property 中,类似于以下:

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

    Spring 支持 5 种自动装配模式,如下:

    • no —— 默认情况下,不自动装配,通过 ref attribute 手动设定。
    • byName —— 根据 Property 的 Name 自动装配,如果一个 bean 的 name,和另一个 bean 中的 Property 的 name 相同,则自动装配这个 bean 到 Property 中。
    • byType —— 根据 Property 的数据类型( Type )自动装配,如果一个 bean 的数据类型,兼容另一个 bean 中 Property 的数据类型,则自动装配。
    • constructor —— 根据构造函数参数的数据类型,进行 byType 模式的自动装配。
    • autodetect —— 如果发现默认的构造函数,用 constructor 模式,否则,用 byType 模式。

    下例中演示自动装配,CustomerService.java 如下:

    package com.shiyanlou.spring.services;
    
    import com.shiyanlou.spring.dao.CustomerDAO;
    
    public class CustomerService {
        CustomerDAO customerDAO;
    
        public void setCustomerDAO(CustomerDAO customerDAO) {
            this.customerDAO = customerDAO;
        }
    
        @Override
        public String toString() {
            return "CustomerService [customerDAO=" + customerDAO + "]";
        }
    
    }
    

    CustomerDAO.java 如下:

    package com.shiyanlou.spring.dao;
    
    public class CustomerDAO {
        @Override
        public String toString(){
            return "Hello , This is CustomerDAO";
        }
    }
    

    Auto-Wiring no

    默认情况下,需要通过 ref 来装配 bean ,如下:

     <bean id = "customerService" class = "com.shiyanlou.spring.services.CustomerService">
      <property name = "customerDAO" ref = "customerDAO" />
      </bean>
     <bean id = "customerDAO" class = "com.shiyanlou.spring.dao.CustomerDAO" />
    

    Auto-Wiring byName

    根据属性 Property 的名字装配 bean,这种情况,CustomerService 设置了 autowire = "byName",Spring 会自动寻找与属性名字 customerDAO 相同的 bean,找到后,通过调用 setCustomerDAO(CustomerDAO customerDAO) 将其注入属性。

     <bean id = "customerService" class = "com.shiyanlou.spring.services.CustomerService" autowire = "byName">
     </bean>
     <bean id = "customerDAO" class = "com.shiyanlou.spring.dao.CustomerDAO" />
    

    如果根据 Property name 找不到对应的 bean 配置,如下:

     <bean id = "customerService" class = "com.shiyanlou.spring.services.CustomerService" autowire = "byName">
     </bean>
     <bean id = "customerDAO_another" class = "com.shiyanlou.spring.dao.CustomerDAO" />
    

    CustomerService 中 Property 名字是 customerDAO,但是配置文件中找不到 customerDAO,只有 customerDAO_another,这时就会装配失败。运行后,CustomerService 中 customerDAO = null。

    Auto-Wiring byType

    根据属性 Property 的数据类型自动装配,这种情况,CustomerService 设置了 autowire = "byType",Spring 会自动寻找与属性类型相同的 bean,找到后,通过调用 setCustomerDAO(CustomerDAO customerDAO) 将其注入。

     <bean id = "customerService" class = "com.shiyanlou.spring.services.CustomerService" autowire = "byType">
     </bean>
     <bean id = "customerDAO" class = "com.shiyanlou.spring.dao.CustomerDAO" />
    

    如果配置文件中有两个类型相同的 bean 会怎样呢?如下:

     <bean id = "customerService" class = "com.shiyanlou.spring.services.CustomerService" autowire = "byType">
     </bean>
     <bean id = "customerDAO" class = "com.shiyanlou.spring.dao.CustomerDAO" />
    <bean id = "customerDAO_another" class = "com.shiyanlou.spring.dao.CustomerDAO" />
    

    一旦配置如上,有两种相同数据类型的 bean 被配置,将抛出 UnsatisfiedDependencyException 异常,见以下:Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException。所以,一旦选择了 byType 类型的自动装配,请确认你的配置文件中每个数据类型定义一个唯一的 bean。

    Auto-Wiring constructor

    这种情况下,Spring 会寻找与参数数据类型相同的 bean,通过构造函数 public Customer(Person person) 将其注入。

     <bean id = "customerService" class = "com.shiyanlou.spring.services.CustomerService" autowire = "constructor">
     </bean>
     <bean id = "customerDAO" class = "com.shiyanlou.spring.dao.CustomerDAO" />
    

    注意:项目中 autowire 结合 dependency-check 一起使用是一种很好的方法,这样能够确保属性总是可以成功注入。如下:

     <bean id = "customerService" class = "com.shiyanlou.spring.services.CustomerService" autowire = "autodetect" dependency-check = "objects">
     </bean>
     <bean id = "customerDAO" class = "com.shiyanlou.spring.dao.CustomerDAO" />
    
  • 相关阅读:
    反射(五)之动态代理的作用
    反射(四)之反射在开发中的适用场景及利弊
    反射(三)之通过反射获取构造方法, 成员变量, 成员方法
    反射(二)之反射机制
    反射(一)之初探反射
    java多线程(五)之总结(转)
    java多线程(四)之同步机制
    java多线程(三)之售票案例
    java多线程(二)之实现Runnable接口
    java多线程(一)之继承Thread类
  • 原文地址:https://www.cnblogs.com/sakura579/p/13926474.html
Copyright © 2011-2022 走看看