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容器有父子關系,相當於全局變量和局部變量的關系!!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM