标签:com public 好的 两种 类型 const 模式 构造 设定
所谓自动装配,就是将一个 Bean 注入到其他 Bean 的 Property 中,类似于以下:
<bean id = "customer" class = "com.shiyanlou.spring.autowire.common.Customer" autowire = "byName" />
Spring 支持 5 种自动装配模式,如下:
下例中演示自动装配,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";
}
}
默认情况下,需要通过 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" />
根据属性 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。
根据属性 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。
这种情况下,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" />
标签:com public 好的 两种 类型 const 模式 构造 设定
原文地址:https://www.cnblogs.com/sakura579/p/13926474.html