spring和springmvc之間的整合

1、springmvc就是運行在spring的環境下,這二者是否須要進行整合,即:是否是要把service 、dao 、 事務 、和其它框架的整合放在springmvc的配置文件中。這樣子在技術層面上是能夠實現的,可是這樣顯得的不清晰。web

2、整合:spring

一、在web.xml中配置contextLoaderListener,而且加入spring的配置文件applicationContext.xmlexpress

這樣能夠把service dao、事務、緩存、以及和其它框架的整合放到spring的配置文件裏面緩存

3、問題mvc

當兩個配置文件中掃描的包有重合的時候某些bean會被初始化2次app

解決:在掃描包的子節點下配置 exclude-filter 和 include-filter框架

在springmvc的配置中:spa

<!-- 配置包掃描 -->
    <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的配置文件中:code

<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>

 

注意:component

在springmvc的handler中能夠引用spring的bean 如service dao,反之則不行,由於spring ioc容器和springmvc ioc容器有父子關係,至關於全局變量和局部變量的關係!!

相關文章
相關標籤/搜索