Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The reasons for the failure of async annotations and how to get request in async

2025-03-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

The reason why async annotation fails and how to get request in async, many novices are not very clear about this, in order to help you solve this problem, the following small series will explain in detail for everyone, people who have this need can learn, I hope you can gain something.

The @EnableAsync annotation is not added to the Application startup class to enable asynchronous; adding the async annotation to the method and the current caller in the same class will cause async to fail: because the asynchronous method called in this method will not be intercepted by the AsyncAnnotationAdvisor aspect for proxy, if it cannot be proxied, then it must still be a normal method, causing asynchronous failure.

After using async annotation, it causes the application to have a circular dependency error

Solution:

Moving async annotation methods to a new service class can be done quickly; of course, you can also use @lazy annotation to deal with classes that cause circular dependencies;

Why is the HttpServletRequest obtained via RequestContextHolder empty in the method of @async annotation?

Because the method annotated by @async will be thrown into the thread pool by springboot to execute, it is equivalent to opening a new thread; but RequestContextHolder uses ThreadLocal to save the request instance, so if you can't get the request in the new thread; if students don't know ThreadLocal, you can learn about it and cut this one that will often be asked during the interview process;

So how do I get request in async?

It is recommended to pass request as an argument to asynchronous methods when invoking asynchronous methods

Did reading the above help you? If you still want to have further understanding of related knowledge or read more related articles, please pay attention to the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report