Spring整合Springmvc的相关介绍
作者:qq_43193797 时间:2022-03-30 10:47:08
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
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
IDEA中的maven没有dependencies解决方案
![](https://img.aspxhome.com/file/2023/1/66641_0s.png)
springboot跨域CORS处理代码解析
![](https://img.aspxhome.com/file/2023/4/69284_0s.png)
DataGridView不显示最下面的新行、判断新增行、删除行操作
Spring中自动装配的4种方式
Hibernate实现悲观锁和乐观锁代码介绍
关于重写equals()方法和hashCode()方法及其简单的应用
![](https://img.aspxhome.com/file/2023/5/57805_0s.jpg)
java.lang.StackOverflowError出现的原因及解决
![](https://img.aspxhome.com/file/2023/8/77148_0s.jpg)
C#实现QQ聊天窗口
C#集合之可观察集合的用法
C# 输出字符串到文本文件中的实现代码
Java的split方法使用详解
![](https://img.aspxhome.com/file/2023/3/64523_0s.png)
WPF模拟实现Gitee泡泡菜单的示例代码
![](https://img.aspxhome.com/file/2023/1/106361_0s.png)
Android开发必备知识 为什么说Kotlin值得一试
SpringMVC MVC架构与Servlet使用详解
![](https://img.aspxhome.com/file/2023/1/61301_0s.png)
Android使用addView动态添加组件的方法
![](https://img.aspxhome.com/file/2023/1/122051_0s.jpg)
将JavaDoc注释生成API文档的操作
![](https://img.aspxhome.com/file/2023/1/61151_0s.png)
Unity3D实现批量下载图片功能
![](https://img.aspxhome.com/file/2023/3/106503_0s.png)
Java对XML文件增删改查操作示例
详解Java中ThreadLocal类型及简单用法
![](https://img.aspxhome.com/file/2023/1/71011_0s.png)