Package org.springframework.transaction.annotation

# Class AnnotationTransactionAttributeSource

java.lang.Object
org.springframework.transaction.interceptor.AbstractFallbackTransactionAttributeSource
org.springframework.transaction.annotation.AnnotationTransactionAttributeSource

All Implemented Interfaces:
  `Serializable`, `org.springframework.beans.factory.Aware`, `org.springframework.context.EmbeddedValueResolverAware`, `TransactionAttributeSource`

public class AnnotationTransactionAttributeSource extends AbstractFallbackTransactionAttributeSource implements Serializable
Implementation of the `TransactionAttributeSource` interface for working with transaction metadata from annotations.

This class reads Spring's `@Transactional` annotation and exposes corresponding transaction attributes to Spring's transaction infrastructure. Also supports JTA's `Transactional` and EJB's `TransactionAttribute` annotation (if present).

This class may also serve as base class for a custom TransactionAttributeSource, or get customized through `TransactionAnnotationParser` strategies.

Since:
  1.2
Author:
  Colin Sampaleanu, Juergen Hoeller
See Also:
  - `Transactional`
  - `TransactionAnnotationParser`
  - `SpringTransactionAnnotationParser`
  - `Ejb3TransactionAnnotationParser`
  - `TransactionAspectSupport.setTransactionAttributeSource(org.springframework.transaction.interceptor.TransactionAttributeSource)`
  - `TransactionProxyFactoryBean.setTransactionAttributeSource(org.springframework.transaction.interceptor.TransactionAttributeSource)`
  - Serialized Form

## Field Summary

### Fields inherited from class org.springframework.transaction.interceptor.AbstractFallbackTransactionAttributeSource

`logger`

## Constructor Summary

Constructors
Constructor
Description
`AnnotationTransactionAttributeSource()`
Create a default AnnotationTransactionAttributeSource, supporting public methods that carry the `Transactional` annotation or the EJB3 `TransactionAttribute` annotation.
`AnnotationTransactionAttributeSource(boolean publicMethodsOnly)`
Create a custom AnnotationTransactionAttributeSource, supporting public methods that carry the `Transactional` annotation or the EJB3 `TransactionAttribute` annotation.
`AnnotationTransactionAttributeSource(TransactionAnnotationParser annotationParser)`
Create a custom AnnotationTransactionAttributeSource.
`AnnotationTransactionAttributeSource(TransactionAnnotationParser... annotationParsers)`
Create a custom AnnotationTransactionAttributeSource.

## Method Summary

Modifier and Type
Method
Description
`void`
`addDefaultRollbackRule(RollbackRuleAttribute rollbackRule)`
Add a default rollback rule, to be applied to all rule-based transaction attributes returned by this source.
`protected boolean`
`allowPublicMethodsOnly()`
By default, only public methods can be made transactional.
`protected TransactionAttribute`
`determineTransactionAttribute(AnnotatedElement element)`
Determine the transaction attribute for the given method or class.
`boolean`
`equals(Object other)`
`protected TransactionAttribute`
`findTransactionAttribute(Class<?> clazz)`
Subclasses need to implement this to return the transaction attribute for the given class, if any.
`protected TransactionAttribute`
`findTransactionAttribute(Method method)`
Subclasses need to implement this to return the transaction attribute for the given method, if any.
`int`
`hashCode()`
`boolean`
`isCandidateClass(Class<?> targetClass)`
Determine whether the given class is a candidate for transaction attributes in the metadata format of this `TransactionAttributeSource`.
`void`
`setPublicMethodsOnly(boolean publicMethodsOnly)`
Set whether transactional methods are expected to be public.

### Methods inherited from class org.springframework.transaction.interceptor.AbstractFallbackTransactionAttributeSource

`computeTransactionAttribute, getCacheKey, getTransactionAttribute, hasTransactionAttribute, setEmbeddedValueResolver`

### Methods inherited from class java.lang.Object

`clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### AnnotationTransactionAttributeSource

public AnnotationTransactionAttributeSource()
Create a default AnnotationTransactionAttributeSource, supporting public methods that carry the `Transactional` annotation or the EJB3 `TransactionAttribute` annotation.

### AnnotationTransactionAttributeSource

public AnnotationTransactionAttributeSource(boolean publicMethodsOnly)
Create a custom AnnotationTransactionAttributeSource, supporting public methods that carry the `Transactional` annotation or the EJB3 `TransactionAttribute` annotation.

Parameters:
  `publicMethodsOnly` - whether to support public methods that carry the `Transactional` annotation only (typically for use with proxy-based AOP), or protected/private methods as well (typically used with AspectJ class weaving)
See Also:
  - `setPublicMethodsOnly(boolean)`

### AnnotationTransactionAttributeSource

public AnnotationTransactionAttributeSource(TransactionAnnotationParser annotationParser)
Create a custom AnnotationTransactionAttributeSource.

Parameters:
  `annotationParser` - the TransactionAnnotationParser to use

### AnnotationTransactionAttributeSource

public AnnotationTransactionAttributeSource(TransactionAnnotationParser... annotationParsers)
Create a custom AnnotationTransactionAttributeSource.

Parameters:
  `annotationParsers` - the TransactionAnnotationParsers to use

## Method Details

### setPublicMethodsOnly

public void setPublicMethodsOnly(boolean publicMethodsOnly)
Set whether transactional methods are expected to be public.

The default is `true`.

Since:
  6.2
See Also:
  - `AnnotationTransactionAttributeSource(boolean)`

### addDefaultRollbackRule

public void addDefaultRollbackRule(RollbackRuleAttribute rollbackRule)
Add a default rollback rule, to be applied to all rule-based transaction attributes returned by this source.

By default, a rollback will be triggered on unchecked exceptions but not on checked exceptions. A default rule may override this while still respecting any custom rules in the transaction attribute.

Parameters:
  `rollbackRule` - a rollback rule overriding the default behavior, for example, `RollbackRuleAttribute.ROLLBACK_ON_ALL_EXCEPTIONS`
Since:
  6.2
See Also:
  - `RuleBasedTransactionAttribute.getRollbackRules()`
  - `EnableTransactionManagement.rollbackOn()`
  - `Transactional.rollbackFor()`
  - `Transactional.noRollbackFor()`

### isCandidateClass

public boolean isCandidateClass(Class<?> targetClass)
Description copied from interface: `TransactionAttributeSource`
Determine whether the given class is a candidate for transaction attributes in the metadata format of this `TransactionAttributeSource`.

If this method returns `false`, the methods on the given class will not get traversed for `TransactionAttributeSource.getTransactionAttribute(java.lang.reflect.Method, java.lang.Class<?>)` introspection. Returning `false` is therefore an optimization for non-affected classes, whereas `true` simply means that the class needs to get fully introspected for each method on the given class individually.

Specified by:
  `isCandidateClass` in interface `TransactionAttributeSource`
Parameters:
  `targetClass` - the class to introspect
Returns:
  `false` if the class is known to have no transaction attributes at class or method level; `true` otherwise. The default implementation returns `true`, leading to regular introspection.
See Also:
  - `TransactionAttributeSource.hasTransactionAttribute(java.lang.reflect.Method, java.lang.Class<?>)`

### findTransactionAttribute

@Nullable protected TransactionAttribute findTransactionAttribute(Class<?> clazz)
Description copied from class: `AbstractFallbackTransactionAttributeSource`
Subclasses need to implement this to return the transaction attribute for the given class, if any.

Specified by:
  `findTransactionAttribute` in class `AbstractFallbackTransactionAttributeSource`
Parameters:
  `clazz` - the class to retrieve the attribute for
Returns:
  all transaction attribute associated with this class, or `null` if none

### findTransactionAttribute

@Nullable protected TransactionAttribute findTransactionAttribute(Method method)
Description copied from class: `AbstractFallbackTransactionAttributeSource`
Subclasses need to implement this to return the transaction attribute for the given method, if any.

Specified by:
  `findTransactionAttribute` in class `AbstractFallbackTransactionAttributeSource`
Parameters:
  `method` - the method to retrieve the attribute for
Returns:
  all transaction attribute associated with this method, or `null` if none

### determineTransactionAttribute

@Nullable protected TransactionAttribute determineTransactionAttribute(AnnotatedElement element)
Determine the transaction attribute for the given method or class.

This implementation delegates to configured `TransactionAnnotationParsers` for parsing known annotations into Spring's metadata attribute class. Returns `null` if it's not transactional.

Can be overridden to support custom annotations that carry transaction metadata.

Parameters:
  `element` - the annotated method or class
Returns:
  the configured transaction attribute, or `null` if none was found

### allowPublicMethodsOnly

protected boolean allowPublicMethodsOnly()
By default, only public methods can be made transactional.

Overrides:
  `allowPublicMethodsOnly` in class `AbstractFallbackTransactionAttributeSource`
See Also:
  - `setPublicMethodsOnly(boolean)`

### equals

public boolean equals(@Nullable Object other)

Overrides:
  `equals` in class `Object`

### hashCode

public int hashCode()

Overrides:
  `hashCode` in class `Object`