Quick revision
The short answer
DevTools runs the application with two class loaders: a base loader for jars that do not change, and a restart loader for the project's own classes. When a class file changes, it throws away the restart loader, creates a new one and restarts the application context, while all the third-party classes stay loaded and JIT-warmed, which is why a restart takes a second or two instead of a full cold start. The common pitfall is an object created by the old restart loader reaching code that expects the new one's classes, which fails with a ClassCastException between two classes with the same name.
What to remember
In this answer, we saw that DevTools splits the class path into a base loader for unchanging jars and a restart loader for the project's classes. On a change, it closes the context, discards the restart loader and starts the application again in a new one, which is fast because library classes stay loaded and JIT-warmed. The classic pitfall is an object from the old restart loader reaching new code through a cache or serialization, which gives a ClassCastException between two loaders, and spring-devtools.properties can move jars between the loaders. DevTools turns itself off for fully packaged applications and is never meant for production.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
DevTools runs the application with two class loaders: a base loader for jars that do not change, and a restart loader for the project's own classes. When a class file changes, it throws away the restart loader, creates a new one and restarts the application context, while all the third-party classes stay loaded and JIT-warmed, which is why a restart takes a second or two instead of a full cold start. The common pitfall is an object created by the old restart loader reaching code that expects the new one's classes, which fails with a ClassCastException between two classes with the same name.
Why this question is asked
Fast feedback while coding is worth a lot, and DevTools is the tool most Spring Boot developers use for it, usually without knowing how it works until it fails in a strange way. Interviewers ask this to see whether we can explain the class loader trick behind it and recognise its classic failure. In this answer, we will recap the two loaders, follow what happens when a file changes, see why it beats a cold start, reproduce the ClassCastException pitfall, and look at how to move jars between loaders and why DevTools stays out of production.
1A Quick Recap of the Two Loaders
Adding spring-boot-devtools as a development-only dependency changes how the application is launched from the IDE or from the build tool. At start-up, DevTools splits the class path into two parts. Jars, such as Spring itself, Hibernate, Jackson and the database driver, are loaded by the base class loader, because they do not change while we work. The project's own output directories, such as target/classes, are loaded by a separate restart class loader, whose parent is the base loader.
Our classes can still see every library class, because the restart loader delegates to the base loader. But nothing in the base loader can see our classes directly, and that asymmetry is the root of the pitfall we will meet below.
- JDK loaders
java.baseand the platform modules - Base class loaderjars: Spring, Hibernate, Jackson; loaded once
- Restart class loadertarget/classes: our controllers, entities, services
3 layers, from the top down. Step through them one at a time.
Only the bottom layer is replaced on each restart.
2What Happens When a File Changes
DevTools watches the class path directories. When we recompile, for example by saving in the IDE with automatic build on, it waits a short quiet period so that a burst of changed files counts as one change, then performs a restart.
7 messages in order. Play them, or step through them one at a time.
So a restart is not a reload of individual classes. Everything in the restart loader, every class of ours and every object of those classes, is thrown away together, and a fresh copy of the application is started inside the same JVM.
3Why It Is Faster Than a Cold Start
A cold start creates a JVM, loads and verifies the classes of every library, runs their static initialisers, and spends the first seconds interpreting code before the JIT compiler warms up. In a typical Spring Boot application, most of the 10,000 to 20,000 loaded classes come from libraries, not from our project.
A restart keeps all of that. The JVM is already running, the library classes in the base loader are already loaded and initialised, and the code paths Spring uses to start a context are already JIT-compiled. Only our classes, usually a few hundred, are loaded again, and the context is rebuilt. A service that takes 8 seconds to start cold often restarts in 1 to 2 seconds. It is still a full context restart, so beans are recreated, connection pools reopen, and in-memory state is lost.
4The Pitfall: Objects From the Old Loader
The trouble starts when an object created by the old restart loader survives the restart. The usual way is through something that lives in the base loader or outside the JVM: a cache library, a static field in a library class, or a serialization round trip.
The two loader identities in the message are the clue: the cached object was created by the previous restart loader, and our code now runs with classes from the new one. The JVM treats them as different types because a class's identity includes its loader. A related case is deserialization: a library in the base loader that deserializes objects, for example from a session store or Redis with Java serialization, may resolve com.acme.Customer through the wrong loader or fail with ClassNotFoundException, because the base loader cannot see our classes. Fixes are to clear such caches on restart, to use the thread context class loader when deserializing, or to move the library that holds our objects into the restart loader.
5Moving Jars Between the Loaders
Sometimes the default split is wrong. A multi-module project may have a sibling module that we edit along with the application but which arrives as a jar, so changes to it do not trigger restarts; or a library may hold our objects and need to be restarted with them. DevTools reads a META-INF/spring-devtools.properties file with restart.include and restart.exclude entries, each a regular expression for jar names.
Included jars move to the restart loader, so they are reloaded and can see our classes. Excluded entries move to the base loader. The keys after include and exclude are just unique names.
6Why DevTools Stays Out of Production
DevTools disables itself when it detects a fully packaged application, for example when it is started with java -jar, or from a special class loader, and Spring Boot's build plugins leave it out of the repackaged jar by default. That matters because it changes defaults, such as disabling template caching, and the remote restart support opens an endpoint that should never be reachable in production. It is a development tool, and a production deployment is always a new process.
7Follow-up questions
How is a DevTools restart different from JVM HotSwap in a debugger?
HotSwap uses the JVM's class redefinition to replace method bodies of already loaded classes in place, so the application keeps running with all its state, but it rejects structural changes such as new fields or methods. A DevTools restart discards all our classes and rebuilds the application context in a fresh loader, so any change works, including new classes, fields and beans, but in-memory state is lost and Spring configuration is re-run. They can be combined: HotSwap for quick method body edits, a DevTools restart for everything else.
Why does an IDE sometimes restart twice for one change?
Because the compiler writes files in several steps, and the watcher may see the first files before the last ones are written. DevTools has a quiet period setting, spring.devtools.restart.quiet-period, that waits for writes to stop, and a trigger file option that restarts only when one chosen file changes.
8Conclusion
In this answer, we saw that DevTools splits the class path into a base loader for unchanging jars and a restart loader for the project's classes. On a change, it closes the context, discards the restart loader and starts the application again in a new one, which is fast because library classes stay loaded and JIT-warmed. The classic pitfall is an object from the old restart loader reaching new code through a cache or serialization, which gives a ClassCastException between two loaders, and spring-devtools.properties can move jars between the loaders. DevTools turns itself off for fully packaged applications and is never meant for production.
Open with
DevTools splits the class path between two class loaders. The base class loader loads the jars, Spring, Hibernate, Jackson and so on, which do not change while I am developing.
Then, if they want more
A restart class loader, whose parent is the base loader, loads my project's own classes from target classes. DevTools watches those directories, and when a class file changes it closes the application context, throws away the restart loader, creates a new one and runs the main class again.
It is faster than a cold start because the JVM keeps running, the third-party classes, which are the large majority, stay loaded and initialised, and the JIT has already compiled the start-up code paths, so only my few hundred classes are loaded again and the context is rebuilt.
A restart that takes a second or two instead of eight is typical. The common pitfall is an object created by the old restart loader surviving the restart, for example in a cache held by a library in the base loader or through Java serialization.
When new code casts it, you get a ClassCastException saying a class cannot be cast to itself, with two RestartClassLoader instances in the message. The fix is to clear such caches or to move the relevant jar into the restart loader with restart.include in META-INF spring-devtools.properties, or exclude things with restart.exclude.
DevTools disables itself for a fully packaged application run with java -jar and is not meant for production.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "How does Spring Boot DevTools restart work, why is it faster than a cold start, and what...": https://scrivio-interview-library.netlify.app/java/class-loading/e1a3885983/