SpringMCV运行流程及整合spring

来源:互联网 发布:淘宝卖家快递推荐 编辑:程序博客网 时间:2024/06/14 12:47

一、Spring整合SpringMVC

@Service用于标注业务层组件

@Controller用于标注控制层组件(如struts中的action)

@Repository用于标注数据访问组件,即DAO组件

@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。

问题: 若 Spring 的 IOC 容器和 SpringMVC 的 IOC 容器扫描的包有重合的部分, 就会导致有的 bean 会被创建 2 次.

解决:

1. 使 Spring 的 IOC 容器扫描的包和SpringMVC 的 IOC 容器扫描的包没有重合的部分. (不推荐)

2. 使用 exclude-filter 和 include-filter 子节点来规定只能扫描的注解

在Spring.xml中,用use-default-filters="false" 来指定不按照默认的扫描,按照自己定义的哪些需要扫描的包。include-filter 表示要的包,exclude-filter 表示除了这个包  <context:component-scan base-package="com.atguigu.springmvc" 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>在beans.xml中,<context:component-scan base-package="com.atguigu.springmvc"><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>

3.注:SpringMVC 的 IOC 容器中的 bean 可以来引用 Spring IOC 容器中的 bean,但是 Spring IOC 容器中的 bean 却不能来引用 SpringMVC IOC 容器中的 bean。


二、springmvc运行流程