由于服务器启动时的加载配置文件的顺序为web.xml---root-context.xml(Spring的配置文件)---servlet-context.xml(SpringMVC的配置文件),由于root-context.xml配置文件中Controller会先进行扫描装配,但是此时service还没有进行事务增强处理,得到的将是原样的Service(没有经过事务加强处理,故而没有事务处理能力),所以我们必须在root-context.xml中不扫描Controller,配置如下:
1
2
3
4
5
6
7
8
|
<!-- 自动扫描组件,这里要把controler下面的 controller去除,他们是在spring3-servlet.xml中配置的,如果不去除会影响事务管理的。 --> <context:component-scan base- package = "com.sence" > <context:exclude-filter type= "annotation" expression= "org.springframework.stereotype.Controller" /> .</context:component-scan> <!-- 自动扫描组件,这里要把controler下面的 controller去除,他们是在spring3-servlet.xml中配置的,如果不去除会影响事务管理的。 --> <context:component-scan base- package = "com.sence" > <context:exclude-filter type= "annotation" expression= "org.springframework.stereotype.Controller" /> </context:component-scan> |
1
2
3
4
5
6
7
8
9
10
|
<!-- 扫描所有的controller 但是不扫描service--> <context:component-scan base- package = "com.sence" > <context:include-filter type= "annotation" expression= "org.springframework.stereotype.Controller" /> <context:exclude-filter type= "annotation" expression= "org.springframework.stereotype.Service" /> </context:component-scan> <!-- 扫描所有的controller 但是不扫描service--> <context:component-scan base- package = "com.sence" > <context:include-filter type= "annotation" expression= "org.springframework.stereotype.Controller" /> <context:exclude-filter type= "annotation" expression= "org.springframework.stereotype.Service" /> </context:component-scan> |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
<!-- transaction manager, use DataSourceTransactionManager --> <bean id= "txManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" > <property name= "dataSource" ref= "dataSource" /> </bean> <!-- spring declarative transaction management --> <aop:config> <aop:pointcut id= "fooServiceMethods" expression= "execution(* com.sence.*.service.impl.*.*(..))" /> <aop:advisor advice-ref= "txAdvice" pointcut-ref= "fooServiceMethods" /> </aop:config> <tx:advice id= "txAdvice" transaction-manager= "txManager" > <tx:attributes> <tx:method name= "find*" read-only= "true" /> <tx:method name= "load*" read-only= "true" /> <tx:method name= "*" rollback- for = "CustomException" /> </tx:attributes> </tx:advice> <!-- transaction manager, use DataSourceTransactionManager --> <bean id= "txManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" > <property name= "dataSource" ref= "dataSource" /> </bean> <!-- spring declarative transaction management --> <aop:config> <aop:pointcut id= "fooServiceMethods" expression= "execution(* com.sence.*.service.impl.*.*(..))" /> <aop:advisor advice-ref= "txAdvice" pointcut-ref= "fooServiceMethods" /> </aop:config> <tx:advice id= "txAdvice" transaction-manager= "txManager" > <tx:attributes> <tx:method name= "find*" read-only= "true" /> <tx:method name= "load*" read-only= "true" /> <tx:method name= "*" rollback- for = "CustomException" /> </tx:attributes> </tx:advice> |
2. 查找Controller扫描部分配置是否正确