Category: spring_boot
Async annotation in Spring Boot
Published on 02 Jun 2026
Explanation
The @Async annotation in Spring Boot
is used to execute a method
asynchronously in a separate thread. This
allows the caller to continue execution
without waiting for the method to
complete, improving application
responsiveness.
Code:
@EnableAsync
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(
Application.class, args);
}
}
Explanation
Annotate a method with @Async to
run it in a background thread.
Code:
@Service
public class EmailService {
@Async
public void sendEmail() {
System.out.println("
Sending email in background...");
}
}
Explanation
Methods annotated with @Async can return
a CompletableFuture for
asynchronous result handling.
Code:
@Async
public CompletableFuture<String>
processData() {
return CompletableFuture.
completedFuture("Data Processed");
}
Explanation
Use @Async for long-running tasks such
as sending emails, generating reports,
processing
files, or calling external APIs.
Code:
@Async
public void generateReport() {
// Long-running report generation
}
Explanation
Without @Async, the caller waits for
the method to finish. With @Async,
the method runs in a separate
thread and the caller continues immediately.
Code:
emailService.sendEmail();
System.out.println("Request completed
without waiting for email.");