SpringMCV运行流程及整合spring

一、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运行流程



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值