基于注解的事务配置--@Transactional
父容器排除 controller
<context:component-scan base-package="com.ant.server"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan>
子容器排除service
<context:component-scan base-package="com.ant.server"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" /> </context:component-scan>
proxy-target-class=true 使用cglib代理service类
<tx:annotation-driven transaction-manager="txManager" proxy-target-class="true" /> <!-- 配置事务管理器 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean>
事务不起作用时,判断servce实例是否是代理类:
AopUtils.isAopProxy(Object object)
AopUtils.isCglibProxy(Object object) //cglib
AopUtils.isJdkDynamicProxy(Object object) //jdk动态代理
debug截图:
DatasourceTransactionManager类断点