Exploring Spring Boot 3.2: Java 21, Virtual Threads, and GraalVM Native Images
Spring Boot 3.2 now fully supports Java 21, Project Loom virtual threads, and GraalVM native images, and this guide walks through installing Java 21‑GraalVM, configuring virtual threads, building sample controllers, async and scheduled tasks, and creating a native executable to achieve faster startup and lower memory usage.
Spring Boot 3.2 has been released, bringing full support for Java 21, virtual threads from Project Loom, and GraalVM native images.
1. Java 21
Spring Boot 3.2 fully supports Java 21, which offers thousands of performance, stability, and security improvements, helping developers boost productivity and drive innovation.
2. Virtual Threads
One of the most important updates is virtual threads, a feature of Project Loom. For details see JEP 444: https://openjdk.org/jeps/444 .
3. GraalVM and Native Images
GraalVM is a high‑performance JDK that can accelerate Java and JVM‑based applications using an alternative JIT compiler. Native Image compiles Java code ahead‑of‑time into a standalone executable that includes application classes, dependencies, runtime libraries, and statically linked native code from the JDK, resulting in faster startup and lower runtime memory usage compared with the JVM.
4. Quick Start
Install Java 21.0.1‑GraalVM using SDKMAN and set it as the default Java version:
sdk install java 21.0.1-graal sdk default java 21.0.1-graalAlternatively, download GraalVM manually from https://www.graalvm.org/downloads/ .
Create a new Spring Boot project (3.2.0) with Java 21, Gradle‑Groovy, Spring Web, and GraalVM native support.
Enable virtual threads in application.yml or application.properties: spring.threads.virtual.enabled:true This setting makes Tomcat use virtual threads for HTTP requests, enables virtual threads for @Async methods, and runs @Scheduled methods on virtual threads.
5. Code Samples
5.1 Simple Controller (Tomcat HTTP request)
@RestController
@RequestMapping("/test")
public class TestController {
private static final Logger log = LoggerFactory.getLogger(TestController.class);
@GetMapping
public void test() {
log.info("Rest controller method has been called {}", Thread.currentThread());
}
}5.2 Asynchronous Task
@Component
public class AsyncTaskExecutorService {
private static final Logger log = LoggerFactory.getLogger(AsyncTaskExecutorService.class);
@Async
public void run() {
log.info("Async task method has been called {}", Thread.currentThread());
}
}5.3 Scheduled Task
@Component
public class SchedulerService {
private static final Logger log = LoggerFactory.getLogger(SchedulerService.class);
@Scheduled(fixedDelayString = "15000")
public void run() {
log.info("Scheduled method has been called {}", Thread.currentThread());
}
}Run the application: ./gradlew bootRun Call the endpoint: curl -X GET 'localhost:8085/test' Sample output shows that the controller, async task, and scheduled task are executed on virtual threads managed by a ForkJoinPool.
Build a GraalVM native image (may take several minutes):
./gradlew nativeCompile
./build/native/nativeCompile/appThe native executable starts much faster and uses less memory, confirming the benefits of GraalVM native images.
The full source code is available at https://github.com/egor-ponomarev/spring-boot3.2-with-graalvm-virtual-threads-example .
Conclusion
Spring Boot 3.2 delivers the long‑awaited support for Java 21, virtual threads, and GraalVM native images, enabling developers to write code with Go‑like performance while retaining the rich Java ecosystem. However, not all libraries are fully compatible with virtual threads, so careful testing is required.
Source: Java技术迷
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
