类 TransactionAspectSupport
- java.lang.Object
- org.springframework.transaction.interceptor.TransactionAspectSupport
- 所有已实现的接口:
Aware
,BeanFactoryAware
,InitializingBean
- 直接已知子类:
TransactionInterceptor
public abstract class TransactionAspectSupport extends Object implements BeanFactoryAware, InitializingBean
Base class for transactional aspects, such as theTransactionInterceptor
or an AspectJ aspect.This enables the underlying Spring transaction infrastructure to be used easily to implement an aspect for any aspect system.
Subclasses are responsible for calling methods in this class in the correct order.
If no transaction name has been specified in the
TransactionAttribute
, the exposed name will be thefully-qualified class name + "." + method name
(by default).Uses the Strategy design pattern. A
PlatformTransactionManager
orReactiveTransactionManager
implementation will perform the actual transaction management, and aTransactionAttributeSource
(e.g. annotation-based) is used for determining transaction definitions for a particular class or method.A transaction aspect is serializable if its
TransactionManager
andTransactionAttributeSource
are serializable.- 从以下版本开始:
- 1.1
- 作者:
- Rod Johnson, Juergen Hoeller, St茅phane Nicoll, Sam Brannen, Mark Paluch
- 另请参阅:
PlatformTransactionManager
,ReactiveTransactionManager
,setTransactionManager(org.springframework.transaction.TransactionManager)
,setTransactionAttributes(java.util.Properties)
,setTransactionAttributeSource(org.springframework.transaction.interceptor.TransactionAttributeSource)
嵌套类概要
嵌套类 修饰符和类型 类 说明 protected static interface
TransactionAspectSupport.InvocationCallback
Simple callback interface for proceeding with the target invocation.protected static class
TransactionAspectSupport.TransactionInfo
Opaque object used to hold transaction information.
构造器概要
构造器 限定符 构造器 说明 protected
TransactionAspectSupport()
方法概要
所有方法 静态方法 实例方法 具体方法 修饰符和类型 方法 说明 void
afterPropertiesSet()
Check that required properties were set.protected void
cleanupTransactionInfo(TransactionAspectSupport.TransactionInfo txInfo)
Reset the TransactionInfo ThreadLocal.protected void
clearTransactionManagerCache()
Clear the transaction manager cache.protected void
commitTransactionAfterReturning(TransactionAspectSupport.TransactionInfo txInfo)
Execute after successful completion of call, but not after an exception was handled.protected void
completeTransactionAfterThrowing(TransactionAspectSupport.TransactionInfo txInfo, Throwable ex)
Handle a throwable, completing the transaction.protected TransactionAspectSupport.TransactionInfo
createTransactionIfNecessary(PlatformTransactionManager tm, TransactionAttribute txAttr, String joinpointIdentification)
Create a transaction if necessary based on the given TransactionAttribute.protected static TransactionAspectSupport.TransactionInfo
currentTransactionInfo()
Subclasses can use this to return the current TransactionInfo.static TransactionStatus
currentTransactionStatus()
Return the transaction status of the current method invocation.protected TransactionManager
determineTransactionManager(TransactionAttribute txAttr)
Determine the specific transaction manager to use for the given transaction.protected BeanFactory
getBeanFactory()
Return the BeanFactory to use for retrievingTransactionManager
beans.TransactionAttributeSource
getTransactionAttributeSource()
Return the transaction attribute source.TransactionManager
getTransactionManager()
Return the default transaction manager, ornull
if unknown.protected String
getTransactionManagerBeanName()
Return the name of the default transaction manager bean.protected Object
invokeWithinTransaction(Method method, Class<?> targetClass, TransactionAspectSupport.InvocationCallback invocation)
General delegate for around-advice-based subclasses, delegating to several other template methods on this class.protected String
methodIdentification(Method method, Class<?> targetClass)
Convenience method to return a String representation of this Method for use in logging.protected TransactionAspectSupport.TransactionInfo
prepareTransactionInfo(PlatformTransactionManager tm, TransactionAttribute txAttr, String joinpointIdentification, TransactionStatus status)
Prepare a TransactionInfo for the given attribute and status object.void
setBeanFactory(BeanFactory beanFactory)
Set the BeanFactory to use for retrievingTransactionManager
beans.void
setTransactionAttributes(Properties transactionAttributes)
Set properties with method names as keys and transaction attribute descriptors (parsed via TransactionAttributeEditor) as values: e.g. key = "myMethod", value = "PROPAGATION_REQUIRED,readOnly".void
setTransactionAttributeSource(TransactionAttributeSource transactionAttributeSource)
Set the transaction attribute source which is used to find transaction attributes.void
setTransactionAttributeSources(TransactionAttributeSource... transactionAttributeSources)
Set multiple transaction attribute sources which are used to find transaction attributes.void
setTransactionManager(TransactionManager transactionManager)
Specify the default transaction manager to use to drive transactions.void
setTransactionManagerBeanName(String transactionManagerBeanName)
Specify the name of the default transaction manager bean.
构造器详细资料
TransactionAspectSupport
protected TransactionAspectSupport()
方法详细资料
currentTransactionInfo
@Nullable protected static TransactionAspectSupport.TransactionInfo currentTransactionInfo() throws NoTransactionException
Subclasses can use this to return the current TransactionInfo. Only subclasses that cannot handle all operations in one method, such as an AspectJ aspect involving distinct before and after advice, need to use this mechanism to get at the current TransactionInfo. An around advice such as an AOP Alliance MethodInterceptor can hold a reference to the TransactionInfo throughout the aspect method.A TransactionInfo will be returned even if no transaction was created. The
TransactionInfo.hasTransaction()
method can be used to query this.To find out about specific transaction characteristics, consider using TransactionSynchronizationManager's
isSynchronizationActive()
and/orisActualTransactionActive()
methods.- 返回:
- the TransactionInfo bound to this thread, or
null
if none - 抛出:
NoTransactionException
- 另请参阅:
TransactionAspectSupport.TransactionInfo.hasTransaction()
,TransactionSynchronizationManager.isSynchronizationActive()
,TransactionSynchronizationManager.isActualTransactionActive()
currentTransactionStatus
public static TransactionStatus currentTransactionStatus() throws NoTransactionException
Return the transaction status of the current method invocation. Mainly intended for code that wants to set the current transaction rollback-only but not throw an application exception.- 抛出:
NoTransactionException
- if the transaction info cannot be found, because the method was invoked outside an AOP invocation context
setTransactionManagerBeanName
public void setTransactionManagerBeanName(@Nullable String transactionManagerBeanName)
Specify the name of the default transaction manager bean.This can either point to a traditional
PlatformTransactionManager
or aReactiveTransactionManager
for reactive transaction management.
getTransactionManagerBeanName
@Nullable protected final String getTransactionManagerBeanName()
Return the name of the default transaction manager bean.
setTransactionManager
public void setTransactionManager(@Nullable TransactionManager transactionManager)
Specify the default transaction manager to use to drive transactions.This can either be a traditional
PlatformTransactionManager
or aReactiveTransactionManager
for reactive transaction management.The default transaction manager will be used if a qualifier has not been declared for a given transaction or if an explicit name for the default transaction manager bean has not been specified.
getTransactionManager
@Nullable public TransactionManager getTransactionManager()
Return the default transaction manager, ornull
if unknown.This can either be a traditional
PlatformTransactionManager
or aReactiveTransactionManager
for reactive transaction management.
setTransactionAttributes
public void setTransactionAttributes(Properties transactionAttributes)
Set properties with method names as keys and transaction attribute descriptors (parsed via TransactionAttributeEditor) as values: e.g. key = "myMethod", value = "PROPAGATION_REQUIRED,readOnly".Note: Method names are always applied to the target class, no matter if defined in an interface or the class itself.
Internally, a NameMatchTransactionAttributeSource will be created from the given properties.
setTransactionAttributeSources
public void setTransactionAttributeSources(TransactionAttributeSource... transactionAttributeSources)
Set multiple transaction attribute sources which are used to find transaction attributes. Will build a CompositeTransactionAttributeSource for the given sources.
setTransactionAttributeSource
public void setTransactionAttributeSource(@Nullable TransactionAttributeSource transactionAttributeSource)
Set the transaction attribute source which is used to find transaction attributes. If specifying a String property value, a PropertyEditor will create a MethodMapTransactionAttributeSource from the value.
getTransactionAttributeSource
@Nullable public TransactionAttributeSource getTransactionAttributeSource()
Return the transaction attribute source.
setBeanFactory
public void setBeanFactory(@Nullable BeanFactory beanFactory)
Set the BeanFactory to use for retrievingTransactionManager
beans.- 指定者:
setBeanFactory
在接口中BeanFactoryAware
- 参数:
beanFactory
- owning BeanFactory (nevernull
). The bean can immediately call methods on the factory.- 另请参阅:
BeanInitializationException
getBeanFactory
@Nullable protected final BeanFactory getBeanFactory()
Return the BeanFactory to use for retrievingTransactionManager
beans.
afterPropertiesSet
public void afterPropertiesSet()
Check that required properties were set.- 指定者:
afterPropertiesSet
在接口中InitializingBean
invokeWithinTransaction
@Nullable protected Object invokeWithinTransaction(Method method, @Nullable Class<?> targetClass, TransactionAspectSupport.InvocationCallback invocation) throws Throwable
General delegate for around-advice-based subclasses, delegating to several other template methods on this class. Able to handleCallbackPreferringPlatformTransactionManager
as well as regularPlatformTransactionManager
implementations andReactiveTransactionManager
implementations for reactive return types.- 参数:
method
- the Method being invokedtargetClass
- the target class that we're invoking the method oninvocation
- the callback to use for proceeding with the target invocation- 返回:
- the return value of the method, if any
- 抛出:
Throwable
- propagated from the target invocation
clearTransactionManagerCache
protected void clearTransactionManagerCache()
Clear the transaction manager cache.
determineTransactionManager
@Nullable protected TransactionManager determineTransactionManager(@Nullable TransactionAttribute txAttr)
Determine the specific transaction manager to use for the given transaction.
methodIdentification
@Nullable protected String methodIdentification(Method method, @Nullable Class<?> targetClass)
Convenience method to return a String representation of this Method for use in logging. Can be overridden in subclasses to provide a different identifier for the given method.The default implementation returns
null
, indicating the use ofDefaultTransactionAttribute.getDescriptor()
instead, ending up asClassUtils.getQualifiedMethodName(Method, Class)
.- 参数:
method
- the method we're interested intargetClass
- the class that the method is being invoked on- 返回:
- a String representation identifying this method
- 另请参阅:
ClassUtils.getQualifiedMethodName(java.lang.reflect.Method)
createTransactionIfNecessary
protected TransactionAspectSupport.TransactionInfo createTransactionIfNecessary(@Nullable PlatformTransactionManager tm, @Nullable TransactionAttribute txAttr, String joinpointIdentification)
Create a transaction if necessary based on the given TransactionAttribute.Allows callers to perform custom TransactionAttribute lookups through the TransactionAttributeSource.
- 参数:
txAttr
- the TransactionAttribute (may benull
)joinpointIdentification
- the fully qualified method name (used for monitoring and logging purposes)- 返回:
- a TransactionInfo object, whether or not a transaction was created. The
hasTransaction()
method on TransactionInfo can be used to tell if there was a transaction created. - 另请参阅:
getTransactionAttributeSource()
prepareTransactionInfo
protected TransactionAspectSupport.TransactionInfo prepareTransactionInfo(@Nullable PlatformTransactionManager tm, @Nullable TransactionAttribute txAttr, String joinpointIdentification, @Nullable TransactionStatus status)
Prepare a TransactionInfo for the given attribute and status object.- 参数:
txAttr
- the TransactionAttribute (may benull
)joinpointIdentification
- the fully qualified method name (used for monitoring and logging purposes)status
- the TransactionStatus for the current transaction- 返回:
- the prepared TransactionInfo object
commitTransactionAfterReturning
protected void commitTransactionAfterReturning(@Nullable TransactionAspectSupport.TransactionInfo txInfo)
Execute after successful completion of call, but not after an exception was handled. Do nothing if we didn't create a transaction.- 参数:
txInfo
- information about the current transaction
completeTransactionAfterThrowing
protected void completeTransactionAfterThrowing(@Nullable TransactionAspectSupport.TransactionInfo txInfo, Throwable ex)
Handle a throwable, completing the transaction. We may commit or roll back, depending on the configuration.- 参数:
txInfo
- information about the current transactionex
- throwable encountered
cleanupTransactionInfo
protected void cleanupTransactionInfo(@Nullable TransactionAspectSupport.TransactionInfo txInfo)
Reset the TransactionInfo ThreadLocal.Call this in all cases: exception or normal return!
- 参数:
txInfo
- information about the current transaction (may benull
)