服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - Java教程 - Spring整合Springmvc的相关介绍

Spring整合Springmvc的相关介绍

2021-07-01 14:5843193797 Java教程

今天小编就为大家分享一篇关于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);

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对服务器之家的支持。如果你想了解更多相关内容请查看下面相关链接

原文链接:https://blog.csdn.net/qq_43193797/article/details/84956869

延伸 · 阅读

精彩推荐