Top Description Fields Constructors Methods
org.springframework.scheduling.annotation

public Class AsyncAnnotationBeanPostProcessor

extends AbstractBeanFactoryAwareAdvisingPostProcessor
Class Inheritance
Annotations
@SuppressWarnings:serial
Imports
java.lang.annotation.Annotation, java.util.concurrent.Executor, java.util.function.Supplier, org.apache.commons.logging.Log, .LogFactory, org.springframework.aop.framework.autoproxy.AbstractBeanFactoryAwareAdvisingPostProcessor, org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler, org.springframework.beans.factory.BeanFactory, org.springframework.core.task.TaskExecutor, org.springframework.lang.Nullable, org.springframework.util.Assert, org.springframework.util.function.SingletonSupplier

Bean post-processor that automatically applies asynchronous invocation behavior to any bean that carries the Async annotation at class or method-level by adding a corresponding AsyncAnnotationAdvisor to the exposed proxy (either an existing AOP proxy or a newly generated proxy that implements all the target's interfaces).

The TaskExecutor responsible for the asynchronous execution may be provided as well as the annotation type that indicates a method should be invoked asynchronously. If no annotation type is specified, this post- processor will detect both Spring's @Async annotation as well as the EJB 3.1 jakarta.ejb.Asynchronous annotation.

For methods having a void return type, any exception thrown during the asynchronous method invocation cannot be accessed by the caller. An AsyncUncaughtExceptionHandler can be specified to handle these cases.

Note

The underlying async advisor applies before existing advisors by default, in order to switch to async execution as early as possible in the invocation chain.

Authors
Mark Fisher, Juergen Hoeller, Stephane Nicoll
Since
3.0
See Also
Async, AsyncAnnotationAdvisor, setBeforeExistingAdvisors, ScheduledAnnotationBeanPostProcessor

Field Summary

Modifier and TypeField and Description
private Class<? extends Annotation>
public static final String
DEFAULT_TASK_EXECUTOR_BEAN_NAME

The default name of the TaskExecutor bean to pick up: "taskExecutor".

private Supplier<AsyncUncaughtExceptionHandler>
private Supplier<Executor>
protected final Log

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
configure(Supplier<Executor> executor, Supplier<AsyncUncaughtExceptionHandler> exceptionHandler)

Configure this post-processor with the given executor and exception handler suppliers, applying the corresponding default if a supplier is not resolvable.

public void
setAsyncAnnotationType(Class<? extends Annotation>
the desired annotation type
asyncAnnotationType
)

Set the 'async' annotation type to be detected at either class or method level.

public void
setBeanFactory(BeanFactory
owning BeanFactory (never null). The bean can immediately call methods on the factory.
beanFactory
)

Overrides org.springframework.aop.framework.autoproxy.AbstractBeanFactoryAwareAdvisingPostProcessor.setBeanFactory.

Implements org.springframework.beans.factory.BeanFactoryAware.setBeanFactory.

Callback that supplies the owning factory to a bean instance.
public void
setExceptionHandler(AsyncUncaughtExceptionHandler exceptionHandler)

Set the AsyncUncaughtExceptionHandler to use to handle uncaught exceptions thrown by asynchronous method executions.

public void
setExecutor(Executor executor)

Set the Executor to use when invoking methods asynchronously.

Inherited from org.springframework.aop.framework.autoproxy.AbstractBeanFactoryAwareAdvisingPostProcessor:
isEligibleprepareProxyFactory

Field Detail

asyncAnnotationTypeback to summary
private Class<? extends Annotation> asyncAnnotationType
Annotations
@Nullable
DEFAULT_TASK_EXECUTOR_BEAN_NAMEback to summary
public static final String DEFAULT_TASK_EXECUTOR_BEAN_NAME

The default name of the TaskExecutor bean to pick up: "taskExecutor".

Note that the initial lookup happens by type; this is just the fallback in case of multiple executor beans found in the context.

Since
4.2
See Also
AnnotationAsyncExecutionInterceptor#DEFAULT_TASK_EXECUTOR_BEAN_NAME
exceptionHandlerback to summary
private Supplier<AsyncUncaughtExceptionHandler> exceptionHandler
Annotations
@Nullable
executorback to summary
private Supplier<Executor> executor
Annotations
@Nullable
loggerback to summary
protected final Log logger

Constructor Detail

AsyncAnnotationBeanPostProcessorback to summary
public AsyncAnnotationBeanPostProcessor()

Method Detail

configureback to summary
public void configure(Supplier<Executor> executor, Supplier<AsyncUncaughtExceptionHandler> exceptionHandler)

Configure this post-processor with the given executor and exception handler suppliers, applying the corresponding default if a supplier is not resolvable.

Since
5.1
setAsyncAnnotationTypeback to summary
public void setAsyncAnnotationType(Class<? extends Annotation> asyncAnnotationType)

Set the 'async' annotation type to be detected at either class or method level. By default, both the Async annotation and the EJB 3.1 jakarta.ejb.Asynchronous annotation will be detected.

This setter property exists so that developers can provide their own (non-Spring-specific) annotation type to indicate that a method (or all methods of a given class) should be invoked asynchronously.

Parameters
asyncAnnotationType:Class<? extends Annotation>

the desired annotation type

setBeanFactoryback to summary
public void setBeanFactory(BeanFactory beanFactory)

Overrides org.springframework.aop.framework.autoproxy.AbstractBeanFactoryAwareAdvisingPostProcessor.setBeanFactory.

Implements org.springframework.beans.factory.BeanFactoryAware.setBeanFactory.

Doc from org.springframework.beans.factory.BeanFactoryAware.setBeanFactory.

Callback that supplies the owning factory to a bean instance.

Invoked after the population of normal bean properties but before an initialization callback such as InitializingBean#afterPropertiesSet() or a custom init-method.

Parameters
beanFactory:BeanFactory

owning BeanFactory (never null). The bean can immediately call methods on the factory.

Annotations
@Override
setExceptionHandlerback to summary
public void setExceptionHandler(AsyncUncaughtExceptionHandler exceptionHandler)

Set the AsyncUncaughtExceptionHandler to use to handle uncaught exceptions thrown by asynchronous method executions.

Since
4.1
setExecutorback to summary
public void setExecutor(Executor executor)

Set the Executor to use when invoking methods asynchronously.

If not specified, default executor resolution will apply: searching for a unique TaskExecutor bean in the context, or for an Executor bean named "taskExecutor" otherwise. If neither of the two is resolvable, a local default executor will be created within the interceptor.

See Also
AnnotationAsyncExecutionInterceptor#getDefaultExecutor(BeanFactory), DEFAULT_TASK_EXECUTOR_BEAN_NAME