1. 是否需要整合 ?
- 不需要 : 單獨使用springmvc. 需要將原先spring中的內容通通遷移到springmvc中. 例如:數據源、事務、aop、service 、dao …
- 需要: 使用spring + springmvc.
- spring : 數據源、事務、aop、service 、dao …
- springmvc : 只負責web相關的.
2. spring容器對象如何進行創建?
非web環境: 直接在main方法 或者是junit測試方法中 通過new classpathxmlapplicationcontext(“spring配置文件”);
web環境 :
springmvc容器如何創建的?
springmvc的容器對象是在web應用服務器啟動時就創建好的.
spring 容器對象如何創建?
- 期望: spring 的容器對象也在web應用服務器啟動時就創建.
- 解決: 使用監聽器 listener
- 思路: 通過監聽器 監聽 servletcontext對象的創建, 因為servletcontext是在web應用服務器啟動時創建的.
當監聽到該事件后,在事件處理方法中將spring的容器對象創建出來.
然后,再將spring的容器對象綁定到servletcontext對象中保存起來,并能共享給web應用服務器中的各個組件.
tips: 監聽器可監聽的對象 以及 監聽的事件
- ① servletcontext(application): 生命周期事件(對象的創建、對象的銷毀)、 數據綁定事件(添加數據、替換數據、移除數據)
- ② httpsession
- ③ httpservletrequest
spring 提供好的監聽器: contextloaderlistener
3. bean被創建兩次的問題:
springmvc : <context:include-filter type=“annotation” expression="@controller" /> use-default-filters= false
spring : <context:exclude-filter type=“annotation” expression="@controller" />
4. spring 與 springmvc 容器的關系 ?
父子關系。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的容器對象?
第一種方式:servletcontext.getattribute(webapplicationcontext.root_web_application_context_attribute);
第二種方式: webapplicationcontextutils.getwebapplicationcontext(servletcontext);
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。如果你想了解更多相關內容請查看下面相關鏈接
原文鏈接:https://blog.csdn.net/qq_43193797/article/details/84956869