您的位置:首页 > 编程语言 > Java开发

Spring整合Springmvc的相关介绍

2019-01-13 00:02 656 查看

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

总结

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

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: