Spring Boot @Async: async execution guide
In Spring Boot, you can achieve asynchronous method invocation by using the @Async annotation. By adding the @Async annotation to a method, Spring Boot will automatically create a new thread to execute that method when it is called, allowing for asynchronous invocation. An example is shown below:
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service
public class MyService {
@Async
public void asyncMethod() {
// 异步方法逻辑
}
}
In the above example, the asyncMethod method is labeled as an asynchronous method. When this method is called, Spring Boot will create a new thread in the background to execute the method without blocking the current thread. To enable asynchronous calling, the @EnableAsync annotation needs to be added to the main class or configuration class.