先在cn.csdn.hr.dao包中建立幾個檔案public interface BaseDao {}public class BaseHibernateDaoImpl implements BaseDao {}public interface CustomerDao {}public class CustomerDaoImpl implements CustomerDao {}cn.csdn.hr.service包中public interface CustomerService {}public class CustomerServiceImpl implements CustomerService {private CustomerDao customerDao;private BaseDao baseDao;//set注入方式public void setCustomerDao(CustomerDao customerDao) {this.customerDao = customerDao;}public void setBaseDao(BaseDao baseDao) {this.baseDao = baseDao;}public CustomerDao getCustomerDao() {return customerDao;}public BaseDao getBaseDao() {return baseDao;}public CustomerServiceImpl(CustomerDao customerDao, BaseDao baseDao) {super();System.out.println("----------------------------------------");this.customerDao = customerDao;this.baseDao = baseDao;}}自動裝配分為五種類型1、Byname:根據屬性名稱自動裝配。此選項將檢查容器並根據名字尋找與屬性完全一致的bean,並將其與屬性自定裝配。例如,在bean定義中將autowire設定為byName,而該bean包含master屬性(同時提供setMaster(。。)方法),spring就會自動尋找名為master的bean定義,並用它來裝配給master屬性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.5.xsd"> <!-- autowire 自動裝配bean的配置屬性 byName 根據名稱自動裝配 cn.csdn.hr.service.CustomerServiceImpl 屬性名稱 與 bean id的名稱一致的時候 就自動裝配 --> <bean id="customerServiceImpl" class="cn.csdn.hr.service.CustomerServiceImpl" autowire="byName"/> <bean id="customerDao" class="cn.csdn.hr.dao.CustomerDaoImpl"/> <bean id="baseDao" class="cn.csdn.hr.dao.BaseHibernateDaoImpl"/> </beans>2、byType:如果容器中存在一個指定屬性類型相同的bean,那麼將與該屬性自動裝配。如果存在多個該類型的bean,或者缺少該類型的bean,注入的全部是null<?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.5.xsd"> <!-- autowire 自動裝配bean的配置屬性 byType根據類型自動裝配 cn.csdn.hr.service.CustomerServiceImpl 屬性 類型 與 bean 中有相同類型的時候 就自動裝配 --> <bean id="customerServiceImpl" class="cn.csdn.hr.service.CustomerServiceImpl" autowire="byType"/> <bean id="customerDaoImpl" class="cn.csdn.hr.dao.CustomerDaoImpl"/> <bean id="baseDaoImpl" class="cn.csdn.hr.dao.BaseHibernateDaoImpl"/> 3、constructor:在容器中自動尋找與需要自動裝配的bean的構造方法參數一致的一個或多個bean。如存在不確定的bean 或構造方法,容器會拋出異常<?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.5.xsd"> <!-- autowire 自動裝配bean的配置屬性 constructor 根據類型自動裝配 是構造器的參數 cn.csdn.hr.service.CustomerServiceImpl 構造器參數類型 與 bean 中有相同類型的時候 就自動裝配 --> <bean id="customerServiceImpl" class="cn.csdn.hr.service.CustomerServiceImpl" autowire="constructor"/> <bean id="customerDaoImpl" class="cn.csdn.hr.dao.CustomerDaoImpl"/> <bean id="baseDaoImpl" class="cn.csdn.hr.dao.BaseHibernateDaoImpl"/> </beans>4、autodetect:首先嘗試使用constructor來自動裝配,然後使用byType方式。不確定性處理與constructor和byType方式一樣<?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.5.xsd"> <!-- autowire 自動裝配bean的配置屬性 autodetect cn.csdn.hr.service.CustomerServiceImpl 有沒有預設的構造 預設的構造器 就採用byType類型 如果沒有則採用constructor --> <bean id="customerServiceImpl" class="cn.csdn.hr.service.CustomerServiceImpl" autowire="autodetect"/> <bean id="customerDaoImpl" class="cn.csdn.hr.dao.CustomerDaoImpl"/> <bean id="baseDaoImpl" class="cn.csdn.hr.dao.BaseHibernateDaoImpl"/> </beans>5、第五種是空值的情況