<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>

          面試官:你能說(shuō)說(shuō)Spring框架中Bean的生命周期嗎?

          共 4044字,需瀏覽 9分鐘

           ·

          2021-03-26 07:58


          首先簡(jiǎn)單說(shuō)一下(以下為一個(gè)回答的參考模板)

          1、實(shí)例化一個(gè)Bean--也就是我們常說(shuō)的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)過(guò)清理階段,如果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通常通過(guò)配置文件定義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.0.xsd”>


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

          </beans>

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

          2、Bean的初始化

          有兩種方式初始化Bean。

          1、在配置文檔中通過(guò)指定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="向全世界問(wèn)好!";
                 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的方法,然后在配置文件中通過(guò) 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方法。

          圖解



          往期資源  需要請(qǐng)自取

          真香警告!Alibaba珍藏版mybatis手寫文檔,刷起來(lái)

          臥槽!字節(jié)跳動(dòng)《算法中文手冊(cè)》火了,完整版 PDF 開(kāi)放下載

          字節(jié)跳動(dòng)總結(jié)的設(shè)計(jì)模式 PDF 火了,完整版開(kāi)放下載!

          堪稱神級(jí)的Spring Boot手冊(cè),從基礎(chǔ)入門到實(shí)戰(zhàn)進(jìn)階

          臥槽!阿里大佬總結(jié)的《圖解Java》火了,完整版PDF開(kāi)放下載!

          喜歡就"在看"唄^_^

          瀏覽 38
          點(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>
                  美女视频黄a视频全免费不卡 | 亚洲777| 日日夜夜噜 | 精品国产视频 | 日韩一级片在现观看视频 |