---
title: "spring事务配置"
author: "haotianpeng163"
date: "2012-10-26T16:39:10Z"
tags: []
categories: ["企业架构"]
slug: "spring"
draft: false
lastmod: "2012-10-26T16:39:10Z"
id: 1706191
url: "https://howeiyue.iteye.com/blog/1706191"
---

1、基于JDK动态代理：
 
java代码：
<tx:annotation-driven transaction-manager="txManager"/>
   该配置方式默认就是JDK动态代理方式

@Transactional()
可以将注解放到接口上


2、基于CGLIB类代理：
 
j
<tx:annotation-driven transaction-manager="txManager" proxy-target-class="true"/>
 
将注解放在具体类上或具体类的实现方法上才会起作用

3、基于aspectj的

<tx:annotation-driven transaction-manager="txManager" mode="aspectj" proxy-target-class="true"/>


基于JDK动态代理 ，可以将@Transactional放置在接口和具体类上。

基于CGLIB类代理，只能将@Transactional放置在具体类上。

因此 在实际开发时全部将@Transactional放到具体类上，而不是接口上。

实现：

JDK动态代理
Spring使用JdkDynamicAopProxy实现代理

CGLIB动态代理
通过Cglib2AopProxy实现代理，而内部通过DynamicAdvisedInterceptor进行拦截

Spring使用AnnotationTransactionAttributeSource通过查找一个类或方法是否有@Transactional注解事务来返回TransactionAttribute（表示开启事务）：



http://www.2cto.com/kf/201203/122445.html
