简体   繁体   中英

Spring Boot - Possible to intercept @Async service methods?

I currently have a Spring service with an @Async method, so when it is called, Spring executes it in a new thread without the request attributes of the parent thread.

I need a way to copy these attributes across to the child thread when calling the async service.

Is there some way to add some kind of interceptor class that Spring will call when creating the new thread, where I could put logic to copy across relevant context values?

Ideally I want to avoid passing the values in a method parameter.

What you need is AsyncHandlerInterceptor and here is a detailed example on how to actualy do that.

Example asynchandler

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM