其中有一行:
Class beanClass = getBeanClass(element);
getBeanClass是在TxAdviceBeanDefinitionParser中实现的,很简单:
@Override
protected Class getBeanClass(Element element) {
return TransactionInterceptor.class;
}
至此,这个标签解析的流程已经基本清晰了。那就是:解析除了一个以TransactionInerceptor为classname的beandefinition并且注册这个bean。剩下来要看的,就是这个TranscationInterceptor到底是什么?
看看这个类的接口定义,就明白了:
public class TransactionInterceptor extends TransactionAspectSupport implements MethodInterceptor, Serializable

这根本就是一个spring AOP的advice嘛!现在明白为什么事务的配置能通过aop产生作用了吧?
下体看看这个advice的advice:
public Object invoke(final MethodInvocation invocation) throws Throwable {
// Work out the target class: may be <code>null</code>.
// The TransactionAttributeSource should be passed the target class
// as well as the method, which may be from an interface.
Class targetClass = (invocation.getThis() != null ? invocation.getThis().getClass() : null);
// If the transaction attribute is null, the method is non-transactional.
final TransactionAttribute txAttr =
getTransactionAttributeSource().getTransactionAttribute(invocation.getMethod(), targetClass);
final PlatformTransactionManager tm = determineTransactionManager(txAttr);
final String joinpointIdentification = methodIdentification(invocation.getMethod());
if (txAttr == null || !(tm instanceof CallbackPreferringPlatformTransactionManager)) {
// Standard transaction demarcation with getTransaction and commit/rollback calls.
TransactionInfo txInfo = createTransactionIfNecessary(tm, txAttr, joinpointIdentification);
Object retVal = null;
try {
// This is an around advice: Invoke the next interceptor in the chain.
// This will normally result in a target object being invoked.
retVal = invocation.proceed();
}
catch (Throwable ex) {
// target invocation exception
completeTransactionAfterThrowing(txInfo, ex);
throw ex;
}
finally {
cleanupTransactionInfo(txInfo);
}
commitTransactionAfterReturning(txInfo);
return retVal;
}
else {
// It's a CallbackPreferringPlatformTransactionManager: pass a TransactionCallback in.
try {
Object result = ((CallbackPreferringPlatformTransactionManager) tm).execute(txAttr,
new TransactionCallback<Object>() {
public Object doInTransaction(TransactionStatus status) {
TransactionInfo txInfo = prepareTransactionInfo(tm, txAttr, joinpointIdentification, status);
try {
return invocation.proceed();
}
catch (Throwable ex) {
if (txAttr.rollbackOn(ex)) {
// A RuntimeException: will lead to a rollback.
if (ex instanceof RuntimeException) {
throw (RuntimeException) ex;
}
else {
throw new ThrowableHolderException(ex);
}
}
else {
// A normal return value: will lead to a commit.
return new ThrowableHolder(ex);
}
}
finally {
cleanupTransactionInfo(txInfo);
}
}
});
// Check result: It might indicate a Throwable to rethrow.
if (result instanceof ThrowableHolder) {
throw ((ThrowableHolder) result).getThrowable();
}
else {
return result;
}
}
catch (ThrowableHolderException ex) {
throw ex.getCause();
}
}
}
哦,原来就是在这里控制了method invocation(spring aop是基于method的)!根据我们的配置,来决定
是不是对method使用事务。
办公室的基本任务是参与学校政务、管理事务、搞好服务,具体地说有:辅助决策、管理事务、起草学校方案文本材料、协调各办公室及同事关系、调查研究、收集信息、制定规章制度、管理会议、收发信函、接待来访、招待客人、安排车辆、整理文档等。mybatis事务的创建是交给transactionfactory 事务工厂来创建的,如果我们将
然后在aop中配置pointcut和这个advice,就能产生作用了!
当然,这里没有分析事物控制的细节以及spring aop的原理,这些在接下来的章节里面会慢慢补全~
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-115475-2.html
如果钱比较闲放招财宝一年有500多
现在世界新格局已经形成了