<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                [TOC] # 延遲初始化 當 bean 的作用域為 singleton 時,bean 對象是在 Spring 容器啟動時就進行創建了。即默認情況下會在容器啟動時初始化 bean. 但我們也可以指定 bean 節點的 `lazy-init="true"`來延遲初始化 bean,這時候,只有第一次獲取 bean 會才初始化 bean ~~~ <bean name="user" class="com.spring.User" scope="singleton" lazy-init="true"> <property name="name" value="jelly" /> </bean> ~~~ 如果想對所有 bean 都應用延遲初始化,可以在根節點 beans 設置 `default-lazy-init="true"`,如下: ~~~ <?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.xsd" default-lazy-init="true"> ...... </beans> ~~~ # init-method/destory-method 我們希望在 bean 被初始化的時候,就初始化某些資源。為了達到這樣的目的,我們可修改代碼為: 在類中添加 ~~~ public void init() { System.out.println("我是初始化方法!"); } public void destory() { System.out.println("我是銷毀方法!"); } ~~~ 這樣,我們的目的就具體地成為:當 Spring 容器初始化對象之后,就要執行該對象的 init() 方法。為了達成這樣的目的,只須修改 Spring 的配置文件內容為: ~~~ <bean name="user" class="com.spring.User" scope="singleton" lazy-init="false" init-method="init"> <property name="name" value="jelly" /> </bean> ~~~ 現在我們又希望在 bean 被銷毀的時候,就釋放或關閉某些資源 ~~~ <bean name="user" class="com.spring.User" scope="singleton" lazy-init="false" destroy-method="destory"> <property name="name" value="jelly" /> </bean> ~~~ 測試方法 ~~~ ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); Object user = ac.getBean("user"); System.out.println(user); //正常關閉spring容器 ac.close(); ~~~ # InitializingBean/DisposableBean 在 spring 容器初始化 bean 和銷毀前所做的操作 bean的所有屬性都設置完畢后BeanFactory會調用該方法。如果在bean里指定了init-method初始化方法,BeanFactory會先調用afterPropertiesSet然后再調用init-method指定的方法 在單例bean被銷毀之前調用destroy()方法。如果在bean里指定了destroy-method銷毀方法,BeanFactory會先調用destroy然后再調用destroy-method指定的方法。注意destroy指的是單例,因為當指定指定的生命周期是prototype時,bean由客戶端自己管理,容器不在管理bean **注意:destroy-method只對scope=singleton有效果** ~~~ package com.spring; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; public class MyService implements InitializingBean, DisposableBean{ @Override public void afterPropertiesSet() throws Exception { System.out.println("InitializingBean"); } @Override public void destroy() throws Exception { System.out.println("DisposableBean"); } } ~~~ 配置文件 ~~~ <bean id="serviceInit" class="com.spring.MyService"> </bean> ~~~ # 生命周期 * 構造 * postProcessBeforeInitialization * afterPropertiesSet * init ![](https://box.kancloud.cn/7119ae0c6b20435c1d6e9be92c5a4519_907x668.png) 1. instantiate bean對象實例化 2. populate properties 封裝屬性 3. 如果Bean實現BeanNameAware執行setBeanName 4. 如果Bean實現BeanFactoryAwar或ApplicationContextAwar設置工廠setBeanFactory或上下文對象setApplicationContext 5. 如果存在類實現BeanPostProcessor(后處理Bean),執行postProcessBeforeInitialization 6. 如果Bean實現InitializingBean執行afterPropertiesSet 7. 調用自定義的init-method方法 8. 如果存在類實現BeanPostProcessor(處理Bean),執行postProcessAfterInitialization 9. 執行業務處理 10. 如果Bean實現DisposableBean執行destroy 11. 調用自定義的destroy-method 對于bean的生命周期方法: 第三步與第四步是讓Bean了解spring容器。 第五步與第八步 可以針對指定的Bean進行功能增強,這時一般會使用動態代理. 第六步與第十步:通過實現指定的接口來完成init與destroy操作 但是在開發中一般不使用第6步與第10步,原因是我們可以使用第7步與第11步來完成。 第7步與第11步的初始化與銷毀操作它無耦合,推薦使用的。但是必須在配置文件中指定初始化與銷毀的方法 ~~~ <bean name="user" class="studySpring.User" init-method="init" destroy-method="destory"></bean> ~~~ 然后在User類中寫init和destory方法 然后再寫測試方法 ~~~ // 1.創建容器對象,因為直接在src下,就寫applicationContext.xml ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); // 2.向容器要User對象 Object u = ac.getBean("user"); // 打印User對象 System.out.println(u); // 關閉容器,觸發銷毀方法,close方法在ClassPathXmlApplicationContext中 ac.close(); ~~~ 總結: 對于bean的生命周期,我們需要關注的主要有兩個方法: 1. 增強bean的功能可以使用后處理Bean, BeanPostProcessor 2. 如果需要初始化或銷毀操作我們可以使用init-method destroy-method **注意:destroy-method只對scope="singleton"有效果,而且是關閉容器時才觸發** # 執行流程 ![](https://box.kancloud.cn/92d36fc67560fbece2f85c048218b04a_172x903.png) Spring 容器中的 Bean 是有生命周期的,Spring 允許在 Bean 在初始化完成后以及 Bean 銷毀前執行特定的操作,常用的設定方式有以下三種: 1. 通過實現 InitializingBean/DisposableBean 接口來定制初始化之后/銷毀之前的操作方法; 2. 通過 `<bean>` 元素的 init-method/destroy-method屬性指定初始化之后 /銷毀之前調用的操作方法; 3. 在指定方法上加上@PostConstruct 或@PreDestroy注解來制定該方法是在初始化之后還是銷毀之前調用 但他們之前并不等價。即使3個方法都用上了,也有先后順序. ~~~ Constructor > @PostConstruct >InitializingBean > init-method ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看