Enable Javascript

Please enable Javascript to view website properly

Toll Free 1800 889 7020

Looking for an Expert Development Team? Take two weeks Trial! Try Now

Hot Swapping in Spring Boot applications

Technology

In the present world, the majority of IDEs support 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 reloads the method bodies, which means if we add any new methods, add annotation or change 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 agents for hot-reloading applications. It has no restrictions like embedded JDK HotSwap. It supports existing features provided by embedded JDK. Simultaneously it will reload to add, modify or remove methods and add or change method signatures.

All modern build tools like maven and Gradle provides a plugin for the Spring Loaded Java agent. We can include it as a dependency and start development without the 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 are one of the dependencies using which it will provide additional development features 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 a 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, another project will depend on this project and will not devtools as a dependency.

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

Default Properties

Spring boot provides various caching features to provide the best performance of the application. It will use compiled format all templates used different template engines like velocity, free maker, thyme leaf, etc.

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

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

Automatic Restart

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

Using these devtools we can reduce the waiting time to reflect developer changes, which does automatically whenever developers save changes. The changes will be auto-deploy to the application/web server.

Spring boot will maintain two classloaders. One classloader will load the dependent library classes, and another classloader will responsible for loading the project class files.

Whenever a developer will make any changes to code, upon saving the second classloader will be reloading all the classes. So startup time for the application drastically decreased.

Live Reload:

Using the above approach class-path changes will automatically be deployed, but supposing 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 the server. The browser needs to install a plugin for Live Reload to work as a client for the embedded Live reload the server.

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

Global Settings

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

Debugging Remote applications

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

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

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

Launching Remote Application in debug mode

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

We can also set debug port of the application using the 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 do modify we want the application to be restarted. In such cases, we can use the below property to provide a regex that points to files/folders.

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

Spring Devtools also have 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 this pattern application will not be restarted.

Spring restart using Trigger file

The 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 the trigger file, this file contains whenever the application must have restarted and when not.

We can provide this trigger file using spring.devtools.restart.trigger-file property. We can provide this property as a global property of the spring devtools or we can application.properties file.

Conclusion

Spring Devtools is the developer-friendly dependency that will boost developer work, which will restart the application on the fly using a 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 the watcher to reflect the changes.

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

Recent Blogs

Categories

NSS Note
Some of our clients
team