欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring整合Springmvc的相关介绍

程序员文章站 2024-03-01 23:39:34
1. 是否需要整合 ? 不需要 : 单独使用springmvc. 需要将原先spring中的内容通通迁移到springmvc中. 例如:数据源、事务、ao...

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

总结

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