Spring @Async Not Working

后端 未结 11 689
北荒
北荒 2020-12-08 06:30

An @Async method in a @Service-annotated class is not being called asynchronously - it\'s blocking the thread.

I\'ve got

11条回答
  •  無奈伤痛
    2020-12-08 07:06

    With the help of this excellent answer by Ryan Stewart, I was able to figure this out (at least for my specific problem).

    In short, the context loaded by the ContextLoaderListener (generally from applicationContext.xml) is the parent of the context loaded by the DispatcherServlet (generally from *-servlet.xml). If you have the bean with the @Async method declared/component-scanned in both contexts, the version from the child context (DispatcherServlet) will override the one in the parent context (ContextLoaderListener). I verified this by excluding that component from component scanning in the *-servlet.xml -- it now works as expected.

提交回复
热议问题