Bean的生命週期

來源:互聯網
上載者:User

標籤:des   style   io   ar   os   使用   sp   java   strong   

一.Bean的定義


二.Bean的初始化:

1.實現org.springframework.beans.factory.InitializingBean介面,覆蓋afterPropertiesSet方法.來實現初始化工作

public class ExampleInitalizingBean implements InitializingBean {    @Override    public void aterPropertiesSet() throws Exception {        // do some initializing work    }}



2.配置init-method,根據指定的init方法去執行初始化的工作

<bean id="exampleInitBean" class="example.ExampleBean" init-method="init" />public class Example {    public void init() {        // do some initialization work    }}



3.配置全域預設初始化/銷毀方法,容器中所有的bean在初始化時都會調用init方法,在銷毀時都會調用destory方法

<?xml version="1.0" encoding="UTF-8" ?><beans xmlns="............"       xmlns:xsi="........"       xsi:schemaLocation="....."              default-init-method="init"  default-destory-method="destory" ></beans>


三.Bean的使用


四.Bean的銷毀:

1.實現org.springframework.beans.factory.DisposableBean介面,覆蓋destory方法

2.配置destory-method

(代碼如Bean的初始化)

3.配置全域預設初始化/銷毀方法,容器中所有的bean在初始化時都會調用init方法,在銷毀時都會調用destory方法

<?xml version="1.0" encoding="UTF-8" ?><beans xmlns="............"       xmlns:xsi="........"       xsi:schemaLocation="....."              default-init-method="init"  default-destory-method="destory" ></beans>


當以上三種初始化和銷毀方法同時使用時,配置全域預設初始化和銷毀方法失效(其是一個可選方法,預設的初始化和銷毀方法在類中可有可無).實現介面覆蓋方法的初始化和銷毀方法和配置init-method/destory-method方法同時執行,但是 實現介面覆蓋方法的初始化和銷毀方法要先於 init-method/destory-method方法執行.


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.