EngineerBetter EngineerBetter - 2 months ago 24
Java Question

Spring @Async Not Working


method in a
-annotated class is not being called asynchronously - it's blocking the thread.

I've got
<task: annotation-driven />
in my config, and the call to the method is coming from outside of the class so the proxy should be being hit. When I step through the code, the proxy is indeed hit, but it doesn't seem to go anywhere near any classes related to running in a task executor.

I've put breakpoints in
and they never get hit. I've debugged into
and can see advice getting applied.

The service is defined as an interface (with the method annotated
there for good measure) with the implementation's method annotated
too. Neither are marked

Any ideas what may have gone wrong?


Curiously, it works only when I have my
XML elements in my app-servlet.xml file, and not in my app-services.xml file, and if I do my component scanning over services from there too. Normally I have one XML file with only controllers in it (and restrict the component-scan accordingly), and another with services in it (again with a component-scan restricted such that it doesn't re-scan the controllers loaded in the other file).


<beans xmlns=""
<task:annotation-driven executor="executor" />
<task:executor id="executor" pool-size="7"/>

<!-- Enable controller annotations -->
<context:component-scan base-package="">
<!-- <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> -->

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>

<mvc:annotation-driven conversion-service="conversionService" />

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />

app-services.xml (doesn't work when specified here)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
xmlns:context="" xmlns:task=""

<!-- Set up Spring to scan through various packages to find annotated classes -->
<context:component-scan base-package="">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />

<task:annotation-driven executor="han" />
<task:executor id="han" pool-size="6"/>

Am I missing something glaringly obvious in my configuration, or is there some subtle interplay between config elements going on?

ach ach

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.