Spring和SpringMVC掃描注解類沖突的解決方案
Spring和SpringMVC掃描注解類沖突
最正確的配置方式
在主容器中applicationContext.xml中,將Controller的注解排除掉
<context:component-scan base-package="com"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan>
而在springmvc.xml中,將Service注解給去掉
<context:component-scan base-package="com"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" /> </context:component-scan>
因?yàn)閟pring的context是父子容器,所以會產(chǎn)生沖突,由ServletContextListener產(chǎn)生的是父容器,springMVC產(chǎn)生的是子容器,子容器Controller進(jìn)行掃描裝配時(shí)裝配了@Service注解的實(shí)例,而該實(shí)例理應(yīng)由父容器進(jìn)行初始化以保證事務(wù)的增強(qiáng)處理,所以此時(shí)得到的將是原樣的Service(沒有經(jīng)過事務(wù)加強(qiáng)處理,故而沒有事務(wù)處理能力。
還有一種方式是將service層改用xml配置,其實(shí)這樣做也是變相的讓springmvc無法掃描service,而只能依賴父窗口也就是ServletContextListener來進(jìn)行初始化,這樣同樣被賦予了事務(wù)性。
也可以用直接掃描的方式
直接掃描比較省事,但是事務(wù)回得不到處理,所以在具體的層面上還需要加入注解去聲明事務(wù),比如在dao層和service層加入@Transactional
幾種不同配置的測試
(1)只在applicationContext.xml中配置如下
<context:component-scan base-package="com" />
啟動正常,但是任何請求都不會被攔截,簡而言之就是@Controller失效,出現(xiàn)404錯(cuò)誤
(2)只在springmvc.xml中配置
<context:component-scan base-package="com" />
啟動正常,請求也正常,但是事物失效,也就是不能進(jìn)行回滾
(3)在applicationContext.xml和springmvc.xml中都配置
<context:component-scan base-package="com" />
啟動正常,請求正常,也是事物失效,不能進(jìn)行回滾
(4)在applicationContext.xml中配置如下
<context:component-scan base-package="com.service" />
在springmvc.xml中配置如下
<context:component-scan base-package="com.action" />
或者按最正確的配置applicationContext.xml,將Controller的注解排除掉 ,springmvc.xml,將Service注解給去掉
此時(shí)啟動正常,請求正常,事物也正常了。
Spring和SpringMVC注解掃描注意事項(xiàng)
現(xiàn)象
Springmvc和Spring設(shè)置自動掃描文件夾自動注入bean的時(shí)候有時(shí)候出現(xiàn)沖突
方法
1:springmvc設(shè)置只掃描controller
2:spring設(shè)置不掃描controller
代碼:
springmvc 的掃描設(shè)置只掃描controller spring的掃描設(shè)置為不掃描controller的 防止重復(fù)注入bean管理 (出現(xiàn)未錯(cuò)誤) <!-- 設(shè)置使用Spring注解的類所在的jar包 設(shè)置不掃描下面規(guī)則的 --> <context:component-scan base-package="com.oig"> <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> <!-- 設(shè)置使用SpringMvc注解的類所在的jar包 設(shè)置只掃描自定義規(guī)則的--> <context:component-scan base-package="com.oig" 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>
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
基于Spring Boot使用JpaRepository刪除數(shù)據(jù)時(shí)的注意事項(xiàng)
這篇文章主要介紹了Spring Boot使用JpaRepository刪除數(shù)據(jù)時(shí)的注意事項(xiàng),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06詳解WebSocket+spring示例demo(已使用sockJs庫)
本篇文章主要介紹了WebSocket spring示例demo(已使用sockJs庫),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01基于Java解決華為機(jī)試實(shí)現(xiàn)密碼截取?
這篇文章主要介紹了基于Java解決華為機(jī)試實(shí)現(xiàn)密碼截取,文章圍繞主題相關(guān)資料展開詳細(xì)內(nèi)容,具有一的參考價(jià)值,需要的小伙伴可以參考一下,希望對你有所幫助2022-02-02解決ObjectMapper.convertValue() 遇到的一些問題
這篇文章主要介紹了解決ObjectMapper.convertValue() 遇到的一些問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06