<context:component-scan/>在springMVC.xml和applicatonContext.xml中都有,這裏面配置是有技巧的,不然就容易掉進坑裏。
幾種不同配置的測試:
(1)只在applicationContext.xml中配置如下
<context:component-scan base-package="com" />
啓動正常,但是任何請求都不會被攔截,簡而言之就是@Controller失效,出現404錯誤
(2)只在springmvc.xml中配置
啓動正常,請求也正常,但是事物失效,也就是不能進行回滾
(3)在applicationContext.xml和springmvc.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-filter和exclude-filter,在applicationContext.xml,將Controller的註解排除掉 ,springmvc.xml,將Service註解給去掉,一切正常了。