spring-framework: @Async and @PostConstruct don't work in combination [SPR-7560]

Joern Barthel opened SPR-7560 and commented

How to reproduce

Annotate a void public method with @Async and @PostConstruct. Setup an application context with the appropriate task:executor infrastructure to make @Async work.

Excepted behavior

@PostConstruct method should a) be executed in a different thread and b) not block the initialization of the application context.

Actual behaviour

@PostConstruct method is executed in main Thread and blocks. After method returns Spring bails with an exception:

NoSuchBeanDefinitionException: No matching bean of type [<omitted>] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} 

Workaround

Only known workaround atm. is to manually execute the logic in the @PostConstruct method in a Thread.

Motivation

The application has quite a few state transfers which need to run initially and can easily be parallelized. Business logics accesses the result of those state transfers not necessarily immediately after initialization. Access to asynchronously generated values is easy using patterns such as Google Guava’s ValueFuture<T>.


Affects: 3.0 GA

Issue Links:

  • #12218 Document limitations of @Async annotation

3 votes, 8 watchers

About this issue

  • Original URL
  • State: closed
  • Created 16 years ago
  • Comments: 16 (2 by maintainers)

Most upvoted comments

It felt like an automatic comment, that’s why I wasn’t sure. Also, I thought it wasn’t so unlikely that something had been done about this problem but wasn’t linked to this GitHub issue, given that it was raised almost 10 years ago.

Could someone confirm whether this feature has been added or not? It is unclear why was the issue closed.