spring和springmvc之间的整合

一、springmvc就是运行在spring的环境下,这两者是否需要进行整合,即:是不是要把service 、dao 、 事务 、和其它框架的整合放在springmvc的配置文件中。这样子在技术层面上是可以实现的,但是这样显得的不清晰。

二、整合:

1、在web.xml中配置contextLoaderListener,并且加入spring的配置文件applicationContext.xml

这样可以把service dao、事务、缓存、以及和其它框架的整合放到spring的配置文件里面

三、问题

当两个配置文件中扫描的包有重合的时候某些bean会被初始化2次

解决:在扫描包的子节点下配置 exclude-filter 和 include-filter

在springmvc的配置中:

<!-- 配置包扫描 -->
    <context:component-scan base-package="com.hy.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>

在spring的配置文件中:

<context:component-scan base-package="com.hy.springmvc" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
    </context:component-scan>

 

注意:

在springmvc的handler中可以引用spring的bean 如service dao,反之则不行,因为spring ioc容器和springmvc ioc容器有父子关系,相当于全局变量和局部变量的关系!!

 

转载于:https://www.cnblogs.com/hy87/p/6188862.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值