踩到的坑context:component-scan

<context:component-scan/>springMVC.xmlapplicatonContext.xml中都有,這裏面配置是有技巧的,不然就容易掉進坑裏。

幾種不同配置的測試:

1)只在applicationContext.xml中配置如下

<context:component-scan base-package="com" />

啓動正常,但是任何請求都不會被攔截,簡而言之就是@Controller失效,出現404錯誤

2)只在springmvc.xml中配置

啓動正常,請求也正常,但是事物失效,也就是不能進行回滾

3)在applicationContext.xmlspringmvc.xml中都配置

啓動正常,請求正常,也是事物失效,不能進行回滾

4)在applicationContext.xml中配置如下

<context:component-scan base-package="com.service" />

springmvc.xml中配置如下

<context:component-scan base-package="com.controller" />

此時啓動正常,請求正常,事物也正常了。

<context:component-scan base-package="com" />

啓動正常,請求也正常,但是事物失效,也就是不能進行回滾

<context:component-scan base-package="com" />

5)使用include-filterexclude-filter,在applicationContext.xml,將Controller的註解排除掉 ,springmvc.xml,Service註解給去掉,一切正常了。

 

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章