定义一个注解,用于标注于方法上,标志着此方法是一个事务方法。
@Target ( { ElementType . METHOD , ElementType . TYPE } )
@Retention ( RetentionPolicy . RUNTIME )
public @interface MyTransaction {
}
@Component
@Import ( TransActionBeanPostProcessor . class )
@Target ( { ElementType . TYPE } )
@Retention ( RetentionPolicy . RUNTIME )
@Documented
public @interface EnableTransactionalManagement {
}
BeanPostProcessor Bean的后置处理器,拦截每一个创建后的对象,这里拦截的是Bean的方法上标注了MyTransaction 注解的对象,为其创建代理对象。
public class TransActionBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessAfterInitialization ( Object bean, String beanName) throws BeansException {
MyTransaction annotation = AnnotationUtils . findAnnotation ( bean. getClass ( ) , MyTransaction . class ) ;
boolean isTxProxy = false ;
if ( annotation == null ) {
Method [ ] methods = bean. getClass ( ) . getMethods ( ) ;
for ( Method method : methods) {
annotation = AnnotationUtils . findAnnotation ( method, MyTransaction . class ) ;
if ( annotation != null ) {
isTxProxy = true ;
break ;
}
}
}
if ( isTxProxy) {
bean = TransactionProxyUtil . getTxProxyObject ( bean, transactionManagement ( ) ) ;
}
return bean;
}
@Bean
public PlatformTransactionManagement transactionManagement ( ) {
return new PlatformTransactionManagement ( ) ;
}
}
JdbcTransactionStatus 定义一个事务的状态信息
public class JdbcTransactionStatus implements TransactionStatus {
private Connection connection;
@Override
public void start ( ) {
System . out. println ( "connection.setAutoCommit(false); 开启事务" ) ;
}
@Override
public void commit ( ) {
System . out. println ( "事务提交了..." ) ;
}
@Override
public void rollback ( ) {
try {
System . out. println ( "事务回滚了..." ) ;
} catch ( Exception throwables) {
throwables. printStackTrace ( ) ;
}
}
@Override
public void close ( ) {
System . out. println ( "事务连接关闭了..." ) ;
}
}
PlatformTransactionManagement 定义平台事务管理器,这个事务管理器主要管理DataSource数据源,在开启事务之前将Connection和当前线程绑定。
public class PlatformTransactionManagement implements TransactionManagement {
@Override
public TransactionStatus getTransaction ( ) {
return new JdbcTransactionStatus ( ) ;
}
@Override
public void commit ( TransactionStatus status) {
status. commit ( ) ;
}
@Override
public void rollback ( TransactionStatus status) {
status. rollback ( ) ;
}
@Override
public void close ( TransactionStatus status) {
status. close ( ) ;
}
}
public interface TransactionManagement {
TransactionStatus getTransaction ( ) ;
void commit ( TransactionStatus status) ;
void rollback ( TransactionStatus status) ;
void close ( TransactionStatus transaction) ;
}
public interface TransactionStatus {
void start ( ) ;
void commit ( ) ;
void rollback ( ) ;
void close ( ) ;
}
定义事务代理逻辑,创建代理对象,栏目目标方法,反射执行真正的方法,这里使用的是JDK的动态代理实现的。
public class TransactionProxyUtil {
public static Object getTxProxyObject ( Object source, TransactionManagement transactionManagement) {
Object proxyInstance = Proxy . newProxyInstance ( source. getClass ( ) . getClassLoader ( ) , source. getClass ( ) . getInterfaces ( ) ,
new InvocationHandler ( ) {
@Override
public Object invoke ( Object proxy, Method method, Object [ ] args) throws Throwable {
String name = method. getName ( ) ;
Class < ? > [ ] parameterTypes = method. getParameterTypes ( ) ;
method = source. getClass ( ) . getMethod ( name, parameterTypes) ;
Object result = null ;
if ( ! method. isAnnotationPresent ( MyTransaction . class ) ) {
result = method. invoke ( source, args) ;
} else {
TransactionStatus transaction = transactionManagement. getTransaction ( ) ;
transaction. start ( ) ;
try {
result = method. invoke ( source, args) ;
transactionManagement. commit ( transaction) ;
} catch ( Throwable e) {
transactionManagement. rollback ( transaction) ;
} finally {
transactionManagement. close ( transaction) ;
}
}
return result;
}
} ) ;
return proxyInstance;
}
}