Hot Swapping in Spring Boot applications

Technology:

In the present world all majority IDE`s supporting the HotSwap for web applications using embedded JDK HotSwap Mechanism without restarting the running web/application servers or deploying the application.

hot-swapping

But drawback or limitation of this mechanism is it only reload the method bodies, means if we add any new methods, adding annotation or changing the method signature it will not affect.

For enabling HotSwap in web/standalone based applications JDK will depend on java-agents.

Spring Loaded is one of the JVM agent for hot-reloading the applications. It has no restrictions like embedded JDK HotSwap, it is supporting existing features provided by embedded JDK and it will reload for addition, modifying or removing of methods, andadding or changing method signatures.

All modern build tools like maven, gradle provides a plugin for Spring Loaded Java agent, we can include as dependency and start development without need to restart/redeploy the application for every change.

Required Dependencies:

Add the below dependency in build file(pom.xml)

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency>

Spring-boot-devtools is the one of the dependency using which it will provide additional development feature like excluding the template cache, dynamic reloading of the properties files etc.

These development tools will be disabled, if are in production mode by default, here the production mode means, if we are launching spring boot applications as java -jar , or we are using different class loader means adding this project as a dependent of the other project then it is considered as production mode.

Maven provides one more flag optional to prevent spring-boot-devtools as a transitive dependency, if we specify optional as true, other project will depend on this project will not devtools as dependency.

By default Spring boot flat jar(jar with its dependencies) will not contain devtools.

Default Properties:

Spring boot provides various caching feature to provide best performance of the application, it will use compiled format all templates used different template engines like velocity, freemaker, thymeleaf etc.

the default value for spring.thymeleaf.cache is true in spring boot means caching will be enabled by default, either we need to disable by manually setting it to false, or by adding spring-boot-devtools will automatically do this.

Cache template is one of the example took for explaining the benefits of spring-boot-devtools in development time.

Automatic Restart:

In traditional development cycle, developer will write some code, to reflect the changes either we need to redeploy application, for a small change in code redeploy will take much time.

Using this devtools we can reduce the waiting time to reflect developer changes, which does automatically whenever developer save changes, the changes will auto-deployed to application/web server.

Spring boot will maintain two classloader, one classloader will load the dependent library classes, other classloader will responsible for loading the project class files.

Whenever developer will make any changes to code, upon save second classloader will be reload the all the classes, so that startup time for application drastically decreased.

Live Reload:

Using the above approach class-path changes will automatically deployed, but for suppose if I modify any html, JS code or CSS then we need to refresh the browser every time we change the code. To avoid Springdevtools 1.3 version later it will provide the Live Reload server, browser need to install plugin for Live Reload to work as client for embedded Live Reload server.

All popular browser will have live Reload plugin, we can navigate to http://livereload.com/extensions/ to get browser specific plugins, after installing browser plugin, it will connect to Live Reload server, whenever we change code, Live Reload Server will push changes to client, and refresh the page to reflect the changes.

Global Settings:

The spring-devtools will contain some properties which are not specific to application, the can be located in jar file having spring-devtools.properties, we can also override the default properties by creating file with same name and extensions(with prefixed by “.” Like .spring-devtools.properties) and provide customized properties into it.

Debugging Remote applications:

As we learned above if the application is running in production mode, devtools will not loaded, we can also run the devtools in production by setting one of the configuration propertyexcludeDevtools to falsein Spring-boot Maven plugin.

<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludeDevtools>false</excludeDevtools> </configuration> </plugin>

Which means devtools will added production jar files also, using this devtools debug parameters will be added, and by default 8000 is the default debug port for the application.

Launching Remote Application in debug mode:

Open Launch Configuration in your favorite Editor like IntelliJ or Eclipse, select remote Java application and use org.springframework.boot.devtools.RemoteSpringApplication as the main class and provide the running URL of the application.

We can also set debug port of the application using below property:

spring.devtools.remote.debug.local-port=9000, means 9000 is the debug port of the application.

Watching Additional paths:

Suppose in some cases if some of the files are modified we want the application to be restarted, in such cases we can use below property to provide regex which points to files/folder.

spring.devtools.restart.additional-paths=/depend

Spring Dev tools also has the property which excludes the restart policy if we can change a file or folder in exclude pattern, using spring.devtools.restart.exclude property means if the modified is the this pattern application will not restarted.

Spring restart using Trigger file:

Developer is continuously making changes, we may want to restart at some specific points (not to reload for every change), we can do this using trigger file, this file contain whenever application must have restarted and when not.

We can provide this trigger file using spring.devtools.restart.trigger-file property, we can provides this property as a global property of the spring devtools (as explained above) or we can application.properties file.

Conclusion:

Spring Devtools is the developer friendly dependency which will boost developer work, which will restart the application on the fly using Spring loaded Java agent. Most of the development mode properties will be set by default. Using different properties, we can include/ exclude some of the files from watcher to reflect the changes.

We can also watch the below youtube video for getting more insights.