解決Spring MVC 對AOP不起作用的問題


Spring MVC 和 Spring 整合的時候,SpringMVC的springmvc.xml文件中 配置掃描包,不要包含 service的註解,Spring的applicationContext.xml文件中 配置掃描包時,不要包含controller的註解,如下所示:
SpringMVC的xml配置:
<context:component-scan base-package="com.insigma">
  <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>
Spring MVC啓動時的配置文件,包含組件掃描、url映射以及設置freemarker參數,讓spring不掃描帶有@Service註解的類。爲什麼要這樣設置?因爲springmvc.xmlapplicationContext.xml不是同時加載,如果不進行這樣的設置,那麼,spring就會將所有帶@Service註解的類都掃描到容器中,等到加載applicationContext.xml的時候,會因爲容器已經存在Service類,使得cglib將不對Service進行代理,直接導致的結果就是在applicationContext 中的事務配置不起作用,發生異常時,無法對數據進行回滾。以上就是原因所在。
同樣的在Spring的xml配置如下:
<context:component-scan base-package="com.insigma">           
 <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
掃描包路徑,不掃描帶有@Controller註解的類。因爲這些類已經隨容器啓動時,在springmvc.xml中掃描過一遍了。
完成以上工作

注意以上幾點就OK了。

spring事務調試了一天無解,網上百度事務配置也沒錯。 哭了半天,看到此文章。。。。。
發佈了25 篇原創文章 · 獲贊 46 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章