<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          【02期】你能說說Spring框架中Bean的生命周期嗎?

          共 3029字,需瀏覽 7分鐘

           ·

          2020-07-28 12:42

          程序員的成長(zhǎng)之路
          互聯(lián)網(wǎng)/程序員/技術(shù)/資料共享?
          關(guān)注


          閱讀本文大概需要 3 分鐘。

          來(lái)自:java面試題精選

          首先簡(jiǎn)單說一下(以下為一個(gè)回答的參考模板)
          1、實(shí)例化一個(gè)Bean--也就是我們常說的new;
          2、按照Spring上下文對(duì)實(shí)例化的Bean進(jìn)行配置--也就是IOC注入;
          3、如果這個(gè)Bean已經(jīng)實(shí)現(xiàn)了BeanNameAware接口,會(huì)調(diào)用它實(shí)現(xiàn)的setBeanName(String)方法,此處傳遞的就是Spring配置文件中Bean的id值
          4、如果這個(gè)Bean已經(jīng)實(shí)現(xiàn)了BeanFactoryAware接口,會(huì)調(diào)用它實(shí)現(xiàn)的setBeanFactory(setBeanFactory(BeanFactory)傳遞的是Spring工廠自身(可以用這個(gè)方式來(lái)獲取其它Bean,只需在Spring配置文件中配置一個(gè)普通的Bean就可以);
          5、如果這個(gè)Bean已經(jīng)實(shí)現(xiàn)了ApplicationContextAware接口,會(huì)調(diào)用setApplicationContext(ApplicationContext)方法,傳入Spring上下文(同樣這個(gè)方式也可以實(shí)現(xiàn)步驟4的內(nèi)容,但比4更好,因?yàn)锳pplicationContext是BeanFactory的子接口,有更多的實(shí)現(xiàn)方法);
          6、如果這個(gè)Bean關(guān)聯(lián)了BeanPostProcessor接口,將會(huì)調(diào)用postProcessBeforeInitialization(Object obj, String s)方法,BeanPostProcessor經(jīng)常被用作是Bean內(nèi)容的更改,并且由于這個(gè)是在Bean初始化結(jié)束時(shí)調(diào)用那個(gè)的方法,也可以被應(yīng)用于內(nèi)存或緩存技術(shù);
          7、如果Bean在Spring配置文件中配置了init-method屬性會(huì)自動(dòng)調(diào)用其配置的初始化方法。
          8、如果這個(gè)Bean關(guān)聯(lián)了BeanPostProcessor接口,將會(huì)調(diào)用postProcessAfterInitialization(Object obj, String s)方法、;
          注:以上工作完成以后就可以應(yīng)用這個(gè)Bean了,那這個(gè)Bean是一個(gè)Singleton的,所以一般情況下我們調(diào)用同一個(gè)id的Bean會(huì)是在內(nèi)容地址相同的實(shí)例,當(dāng)然在Spring配置文件中也可以配置非Singleton,這里我們不做贅述。
          9、當(dāng)Bean不再需要時(shí),會(huì)經(jīng)過清理階段,如果Bean實(shí)現(xiàn)了DisposableBean這個(gè)接口,會(huì)調(diào)用那個(gè)其實(shí)現(xiàn)的destroy()方法;
          10、最后,如果這個(gè)Bean的Spring配置中配置了destroy-method屬性,會(huì)自動(dòng)調(diào)用其配置的銷毀方法。
          結(jié)合代碼理解一下

          1、Bean的定義

          Spring通常通過配置文件定義Bean。如:


          <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.0.xsd”>


          <bean?id=”HelloWorld”?class=”com.pqf.beans.HelloWorld”>
          ????<property?name=”msg”>
          ???????<value>HelloWorldvalue>
          ????property>
          bean>

          beans>
          這個(gè)配置文件就定義了一個(gè)標(biāo)識(shí)為 HelloWorld 的Bean。在一個(gè)配置文檔中可以定義多個(gè)Bean。

          2、Bean的初始化

          有兩種方式初始化Bean。

          1、在配置文檔中通過指定init-method 屬性來(lái)完成

          在Bean的類中實(shí)現(xiàn)一個(gè)初始化Bean屬性的方法,如init(),如:
          public?class?HelloWorld{
          ???public?String?msg=null;
          ???public?Date?date=null;

          ????public?void?init()?{
          ??????msg=”HelloWorld”;
          ??????date=new?Date();
          ????}
          ????……?
          }
          然后,在配置文件中設(shè)置init-mothod屬性:

          2、實(shí)現(xiàn) org.springframwork.beans.factory.InitializingBean接口

          Bean實(shí)現(xiàn)InitializingBean接口,并且增加 afterPropertiesSet() 方法:
          public?class?HelloWorld?implement?InitializingBean?{
          ???public?String?msg=null;
          ???public?Date?date=null;

          ???public?void?afterPropertiesSet()?{
          ???????msg="向全世界問好!";
          ???????date=new?Date();
          ???}
          ????……?
          }
          那么,當(dāng)這個(gè)Bean的所有屬性被Spring的BeanFactory設(shè)置完后,會(huì)自動(dòng)調(diào)用afterPropertiesSet()方法對(duì)Bean進(jìn)行初始化,于是,配置文件就不用指定 init-method屬性了。

          3、Bean的調(diào)用

          有三種方式可以得到Bean并進(jìn)行調(diào)用:

          1、使用BeanWrapper

          HelloWorld?hw=new?HelloWorld();
          BeanWrapper?bw=new?BeanWrapperImpl(hw);
          bw.setPropertyvalue(”msg”,”HelloWorld”);
          system.out.println(bw.getPropertyCalue(”msg”));

          2、使用BeanFactory

          InputStream?is=new?FileInputStream(”config.xml”);
          XmlBeanFactory?factory=new?XmlBeanFactory(is);
          HelloWorld?hw=(HelloWorld)?factory.getBean(”HelloWorld”);
          system.out.println(hw.getMsg());

          3、使用ApplicationConttext

          ApplicationContext?actx=new?FleSystemXmlApplicationContext(”config.xml”);
          HelloWorld?hw=(HelloWorld)?actx.getBean(”HelloWorld”);
          System.out.println(hw.getMsg());

          4、Bean的銷毀

          1、使用配置文件中的 destory-method 屬性

          與初始化屬性 init-methods類似,在Bean的類中實(shí)現(xiàn)一個(gè)撤銷Bean的方法,然后在配置文件中通過 destory-method指定,那么當(dāng)bean銷毀時(shí),Spring將自動(dòng)調(diào)用指定的銷毀方法。

          2、實(shí)現(xiàn) org.springframwork.bean.factory.DisposebleBean接口

          如果實(shí)現(xiàn)了DisposebleBean接口,那么Spring將自動(dòng)調(diào)用bean中的Destory方法進(jìn)行銷毀,所以,Bean中必須提供Destory方法。
          圖解


          推薦閱讀:

          【01期】Spring,SpringMVC,SpringBoot,SpringCloud有什么區(qū)別和聯(lián)系?

          5T技術(shù)資源大放送!包括但不限于:C/C++,Linux,Python,Java,PHP,人工智能,單片機(jī),樹莓派,等等。在公眾號(hào)內(nèi)回復(fù)「2048」,即可免費(fèi)獲?。。?/span>

          微信掃描二維碼,關(guān)注我的公眾號(hào)

          寫留言

          朕已閱?

          瀏覽 19
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  成人黄网站18秘 免费看 | 中国a黄片 | 天天干人人摸 | 久久久久久成人无码 | 久久久久亚洲AV成人片乱码 |