Featured

What Is Hot Code Swapping in Java?

A drawback of Spring application development is that, unlike with an application server, changes made to the source code or bytecode aren’t automatically detected after launch.

 

This is because a Spring application typically includes the web server. During development, it’s preferable for modifications made to a controller to be immediately noticeable by reloading the page. This process is commonly referred to as hot code swapping or hot swapping. To accomplish this, there are several approaches. In this post we take a look at two.

 

Hot Swapping of the Java Virtual Machine

The Java virtual machine (JVM) supports hot swapping automatically when a program is running in debug mode. In Eclipse, this runs more smoothly than in IntelliJ because in IntelliJ a build has to be triggered first ((Ctrl) + (F9)). Then you can refresh the browser window (also with (F5)), and a code change of the controller class is visible.

 

However, the debug mode has a few weaknesses. First, only bytecode within methods is exchanged; no initialization routine runs. This means a change of meta information, such as changed paths to annotations, has no effect. Second, changes to configuration files are also not code and don’t cause the bytecode to be replaced.

 

Spring Developer Tools

An alternative to hot swapping is the Spring Developer Tools (dev tools). Here, Spring starts an initial bootstrap program, which in the next step loads the actual application in an extra class loader. This bootstrap class loader registers whether bytecode has been swapped in the background or whether certain central configuration files have been changed. If so, this bootstrap loader will remove the old program and reboot the Spring context. In this case, the entire Spring program is actually restarted, and we lose all volatile state.

 

If you want to try out the dev tools, all you have to do is include a dependency in the class path:

 

<dependency>

   <groupId>org.springframework.boot</groupId>

   <artifactId>spring-boot-devtools</artifactId>

   <optional>true</optional>

</dependency>

 

Another advantage of the development tools is that they can also trigger reloading when selected own resources are changed. The reference documentation explains this in more detail.

 

Because the dev tools reinitialize the entire context, it takes longer to start than solving via the debugger, but hot swapping through the debugger just can’t detect many changes. Teams that want to dig deeper into their pockets can take a look at JRebel.

 

Editor’s note: This post has been adapted from a section of the book Spring Boot 3 and Spring Framework 6 by Christian Ullenboom. Christian is an Oracle-certified Java programmer and has been a trainer and consultant for Java technologies and object-oriented analysis and design since 1997.

 

This post was originally published 6/2025.

Recommendation

Spring Boot 3 and Spring Framework 6
Spring Boot 3 and Spring Framework 6

Say goodbye to dependencies, bogged-down code, and inflexibility! With the Spring framework and Spring Boot, you’ll painlessly create Java applications that are production ready. Start with the basics: containers for Spring-managed beans, Spring framework modules, and proxies. Then learn to connect to relational databases, implement Jakarta Persistence, use Spring Data JPA, and work with NoSQL databases. Get the right know-how for modern software development with Spring and Java!

Learn More
Rheinwerk Computing
by Rheinwerk Computing

Rheinwerk Computing is an imprint of Rheinwerk Publishing and publishes books by leading experts in the fields of programming, administration, security, analytics, and more.

Comments