1. 是否需要整合 ?
- 不需要 : 單獨(dú)使用springmvc. 需要將原先spring中的內(nèi)容通通遷移到springmvc中. 例如:數(shù)據(jù)源、事務(wù)、aop、service 、dao …
- 需要: 使用spring + springmvc.
- spring : 數(shù)據(jù)源、事務(wù)、aop、service 、dao …
- springmvc : 只負(fù)責(zé)web相關(guān)的.
2. spring容器對(duì)象如何進(jìn)行創(chuàng)建?
非web環(huán)境: 直接在main方法 或者是junit測試方法中 通過new classpathxmlapplicationcontext(“spring配置文件”);
web環(huán)境 :
springmvc容器如何創(chuàng)建的?
springmvc的容器對(duì)象是在web應(yīng)用服務(wù)器啟動(dòng)時(shí)就創(chuàng)建好的.
spring 容器對(duì)象如何創(chuàng)建?
- 期望: spring 的容器對(duì)象也在web應(yīng)用服務(wù)器啟動(dòng)時(shí)就創(chuàng)建.
- 解決: 使用監(jiān)聽器 listener
- 思路: 通過監(jiān)聽器 監(jiān)聽 servletcontext對(duì)象的創(chuàng)建, 因?yàn)閟ervletcontext是在web應(yīng)用服務(wù)器啟動(dòng)時(shí)創(chuàng)建的.
當(dāng)監(jiān)聽到該事件后,在事件處理方法中將spring的容器對(duì)象創(chuàng)建出來.
然后,再將spring的容器對(duì)象綁定到servletcontext對(duì)象中保存起來,并能共享給web應(yīng)用服務(wù)器中的各個(gè)組件.
tips: 監(jiān)聽器可監(jiān)聽的對(duì)象 以及 監(jiān)聽的事件
- ① servletcontext(application): 生命周期事件(對(duì)象的創(chuàng)建、對(duì)象的銷毀)、 數(shù)據(jù)綁定事件(添加數(shù)據(jù)、替換數(shù)據(jù)、移除數(shù)據(jù))
- ② httpsession
- ③ httpservletrequest
spring 提供好的監(jiān)聽器: contextloaderlistener
3. bean被創(chuàng)建兩次的問題:
springmvc : <context:include-filter type=“annotation” expression="@controller" /> use-default-filters= false
spring : <context:exclude-filter type=“annotation” expression="@controller" />
4. spring 與 springmvc 容器的關(guān)系 ?
父子關(guān)系。spring是父容器, springmvc是子容器. 子容器可以訪問父容器,反之則不行.
spring : root webapplicationcontext: startup date [tue dec 11 15:34:46 cst 2018]; root of context hierarchy
springmvc : webapplicationcontext for namespace ‘springdispatcherservlet-servlet': startup date [tue dec 11 15:37:11
cst 2018]; parent: root webapplicationcontext
5. 如何在handler的方法中獲取到spring的容器對(duì)象?
第一種方式:servletcontext.getattribute(webapplicationcontext.root_web_application_context_attribute);
第二種方式: webapplicationcontextutils.getwebapplicationcontext(servletcontext);
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)服務(wù)器之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
原文鏈接:https://blog.csdn.net/qq_43193797/article/details/84956869