踩到的坑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注解给去掉,一切正常了。

 

 


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