I have a class in dependency jar which has a method annotated with Spring @Async
and I expect that this method should execute asynchronously in another thread.
Using this class I create a bean in my parent project and wire it to my class which has @Service
annotation. In that class I call a method which has @Async
annotation, but as I said, the method does not execute in another thread.
How can I achieve this?
This is the code.
This is my interface class:
public interface RequestLoggerService {
@Async
void log(HttpServletRequest request, String serviceName, String requestIdentifier, String message, boolean successful);
}
This is the implementation class:
public class RequestLoggerServiceImpl implements RequestLoggerService {
private static Logger logger = Logger.getLogger(RequestLoggerServiceImpl.class);
private RequestLoggerDao requestLogger;
@Override
public void log(HttpServletRequest request, String serviceName, String requestIdentifier, String message, boolean successful) {
logRequest(request, serviceName, request.getRequestURI().substring(request.getRequestURI().lastIndexOf("/") + 1), requestIdentifier, message, successful);
}
}
Those two classes are packed in a jar which is as dependency used in my parent project. I use this class to create a bean in parent project:
<bean id="requestLoggerService" class="com.xyz.service.RequestLoggerServiceImpl">
<property name="requestLogger" ref="requestLoggerDao" />
</bean>
And this bean is then auto-wired in my @Controller
annotated class:
public class ActionsController {
@Autowired
private RequestLoggerService requestLogger;
}
I followed @BretC instructions and found the solution. The problem was that I was instantiating the RequestLoggerService
bean through XML configuration, but used annotation @EnableAsync to enable @Async annotation discovery.
This cant be done, either I need to use annotation driven bean creation, or to enable asynchronous tasks in XML configuration.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.