Spring學習筆記 關於Bean定義的重用-使用abstract以及parent標籤屬性

來源:互聯網
上載者:User

Bean定義的重用主要分為兩步:

一、定義一個父Bean。

二、使子Bean繼承父Bean,並且添加自己的property。

一個父Bean的定義一般如下:

<bean name="templateBean" class="com.ronglei.test.ReuseBean" abstract="true"><property name="beanType" value="testBeanType" /></bean>

通過bean標籤的abstract屬性為true的定義,使此bean不能被執行個體化只能當作模板使用。

當子Bean希望使用此父Bean模板時只需要使用bean標籤的parent屬性進行引用即可,如下:

<bean name="instanceBean" parent="templateBean"><property name="beanId" value="1" /></bean>

 

定義很簡單,子Bean實際上從父Bean中繼承了:

構造方法參數、property的值、method override、init方法定義、destory方法定義、factory方法定義

沒有繼承的內容有:

depends-on定義、autowire定義、dependency-check定義、scope定義、lazy-init定義

依舊是說子Bean從父Bean中繼承了屬性及方法參數,而沒有繼承對bean的屬性以及行為修飾的定義。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.