Spring自動裝配

來源:互聯網
上載者:User
先在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、第五種是空值的情況

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.