diff --git a/spring-framework-reference/src/scheduling.xml b/spring-framework-reference/src/scheduling.xml index 024bf5a4139..6132a409cca 100644 --- a/spring-framework-reference/src/scheduling.xml +++ b/spring-framework-reference/src/scheduling.xml @@ -551,23 +551,26 @@ public void doSomething() {
The @Async Annotation - The @Async annotation can be provided on a method so that - invocation of that method will occur asynchronously. In other words, the - caller will return immediately upon invocation and the actual execution - of the method will occur in a task that has been submitted to a Spring - TaskExecutor. In the simplest case, the annotation may be applied to a - void-returning method. + The @Async annotation can be + provided on a method so that invocation of that method will occur + asynchronously. In other words, the caller will return immediately upon + invocation and the actual execution of the method will occur in a task + that has been submitted to a Spring + TaskExecutor. In the simplest case, the + annotation may be applied to a void-returning + method. @Async void doSomething() { // this will be executed asynchronously } - Unlike the methods annotated with the @Scheduled annotation, these - methods can expect arguments, because they will be invoked in the - "normal" way by callers at runtime rather than from a scheduled task - being managed by the container. For example, the following is a - legitimate application of the @Async annotation. + Unlike the methods annotated with the + @Scheduled annotation, these methods can + expect arguments, because they will be invoked in the "normal" way by + callers at runtime rather than from a scheduled task being managed by + the container. For example, the following is a legitimate application of + the @Async annotation. @Async void doSomething(String s) { @@ -575,15 +578,45 @@ void doSomething(String s) { } Even methods that return a value can be invoked asynchronously. - However, such methods are required to have a Future typed return value. - This still provides the benefit of asynchronous execution so that the - caller can perform other tasks prior to calling 'get()' on that - Future. + However, such methods are required to have a + Future typed return value. This still + provides the benefit of asynchronous execution so that the caller can + perform other tasks prior to calling get() on + that Future. @Async Future<String> returnSomething(int i) { // this will be executed asynchronously } + + @Async can not be used in + conjunction with lifecycle callbacks such as + @PostConstruct. To asynchonously + initialize Spring beans you currently have to use a separate + initializing Spring bean that invokes the + @Async annotated method on the target + then. + + public class SampleBeanImpl implements SampleBean { + + @Async + void doSomething() { … } +} + + +public class SampleBeanInititalizer { + + private final SampleBean bean; + + public SampleBeanInitializer(SampleBean bean) { + this.bean = bean; + } + + @PostConstruct + public void initialize() { + bean.doSomething(); + } +}