SpringMVC和Spring整合的目的;分工明确;

SpringMVC的配置文件就来配置和网站转发逻辑以及网站功能有关的(视图解析器,文件上传解析器,支持ajax,xxx);springmvc.xml

Spring的配置文件来配置和业务有关的(事务控制,数据源,xxx);spring.xml

1. <import resource="spring.xml"/>:可以合并配置文件;虽然是两个spring配置文件,但这种方式仍然相当于一个ioc

2.规范整合

分容器,即两个ioc

1)、在web.xml已经配置SpringMVC的基础上,添加spring容器到web.xml

注意:我们之前配置spring到xml那是一个java项目,并没有web.xml

<!-- needed for ContextLoaderListener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param> <!-- Bootstraps the root web application context before servlet initialization -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

2)、防止两个容器中的组件(只要加注解的组件会被容器创建),被重复创建

SpringMVC.xml与Spring.xml分别加代码

因为Spring管理业务逻辑组件;

排除扫描Controller组件

    <context:component-scan base-package="com.atguigu">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>

因为SpringMVC管理控制器组件;

只扫描Controller组件 要禁用默认行为

<context:component-scan base-package="com.atguigu" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>

3)、注意子父容器问题

Spring是一个父容器,SpringMVC是一个子容器;

子容器还可以引用父容器的组件;

父容器不能引用子容器的组件;

注意调用方式,按正常的调用方式即可Controller调Service

最新文章

  1. SVNKit支持SSH连接
  2. 在ubuntu/deepin/mint等系统中使用命令删除文件或文件夹
  3. 算法:POJ1008 Maya Calendar
  4. Open Source Cassandra Gitbook for Developer
  5. Hexo部署到GitHub出现spawn ENOENT的解决办法
  6. url rewrite
  7. Watch gcc at ubuntu 12,See ELF file header
  8. perf---LINUX内核研究
  9. Ant—Ant标签解释
  10. Springboot与Thymeleaf模板引擎整合基础教程(附源码)
  11. FastDFS与Nginx的配置说明
  12. SpringCloud学习之SpringCloudStream&amp;集成kafka
  13. 深入理解DirectByteBuffer
  14. Juint单元测试
  15. Golang 入门 : 字符串
  16. C# 主动发起请求代码
  17. py2与py3区别总结
  18. MHA官方文档翻译
  19. freeRTOS中文实用教程3--中断管理之计数信号量
  20. Redux-example

热门文章

  1. 前端每日实战:50# 视频演示如何用纯 CSS 创作一个永动的牛顿摆
  2. 前端每日实战:76# 视频演示如何用纯 CSS 创作一组单元素办公用品(内含2个视频)
  3. 初识java-1.Java跨平台的原理
  4. C++ 概率算法 利用蒙特卡罗算法计算圆周率
  5. iOS系统日历选择问题
  6. set实现数组去重后是对象,这里转化为数组
  7. vue之条件语句小结
  8. Original blog
  9. docker 应用
  10. ceph-cluster map