SoFunction
Updated on 2025-03-08

Brief description of Springboot @Async asynchronous method

1. Asynchronous call

Asynchronous calls are to execute high-time-consuming methods without blocking the main thread

2. Regular asynchronous

Implemented by opening a new thread

3. Enable asynchronous methods in Springboot

Requires 4 comments

1.@EnableAsyncTurn on asynchronous
2.@ComponentRegister asynchronous components
3.@AsyncAnnotate asynchronous methods
4.@AutowiredInject asynchronous components

4. Make an asynchronous call

1. First, mark the asynchronous on a Config class
2. Then create an asynchronous component class, just like Service or Controller, labeled with Component, and Service is OK.
3. Create an asynchronous method in the class and mark it with the Async tag. This method must be an instance method.
4. Then it's the same as injecting the Service.

5. Asynchronous transactions

It is useless to label @Transactional on the Async method.
The appoint @Transactional on the Service called by the Async method is valid.

6. Internal call of asynchronous method

Asynchronous methods do not support internal calls, that is, asynchronous methods cannot be written inside the class that needs to be called.
For example, Class A has a, b, and c. b has Async annotation. At this time, the asynchronous call of a to b is invalid.

7. Why must an asynchronous method be an instance method

Because the static method cannot be override. Because the implementation principle of @Async asynchronous method is to inject a proxy class into a bean, which inherits the bean and needs to overwrite the asynchronous method and execute it.
 

Then this thing will be placed in a queue maintained by Spring. Wait for the thread pool to read and execute.