由于服務(wù)器啟動(dòng)時(shí)的加載配置文件的順序?yàn)閣eb.xml---root-context.xml(Spring的配置文件)---servlet-context.xml(SpringMVC的配置文件),由于root-context.xml配置文件中Controller會(huì)先進(jìn)行掃描裝配,但是此時(shí)service還沒有進(jìn)行事務(wù)增強(qiáng)處理,得到的將是原樣的Service(沒有經(jīng)過事務(wù)加強(qiáng)處理,故而沒有事務(wù)處理能力),所以我們必須在root-context.xml中不掃描Controller,配置如下:
1
2
3
4
5
6
7
8
|
<!-- 自動(dòng)掃描組件,這里要把controler下面的 controller去除,他們是在spring3-servlet.xml中配置的,如果不去除會(huì)影響事務(wù)管理的。 --> <context:component-scan base- package = "com.sence" > <context:exclude-filter type= "annotation" expression= "org.springframework.stereotype.Controller" /> .</context:component-scan> <!-- 自動(dòng)掃描組件,這里要把controler下面的 controller去除,他們是在spring3-servlet.xml中配置的,如果不去除會(huì)影響事務(wù)管理的。 --> <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掃描部分配置是否正確