Spring 整合 Springmvc

编程入门 行业动态 更新时间:2024-10-24 20:18:29

<a href=https://www.elefans.com/category/jswz/34/1769862.html style=Spring 整合 Springmvc"/>

Spring 整合 Springmvc

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

更多推荐

Spring 整合 Springmvc

本文发布于:2023-07-28 17:39:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1265228.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Spring   Springmvc

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!