Actually almost all modern IDEs (IntelliJ, Eclipse, etc.) gives you support for
reloading classes without need to restart the whole application
using of JDK
Unfortunately, HotSwap has limitations and can reload only changes in method bodies and don’t support changing method or class signatures ( at the moment ).
Spring Loaded to the rescue
Spring Loaded is a JVM agent that is actually used as reloading system in
Grails 2.x and
has no restrictions to reload changes only in method bodies like HotSwap in JDK.
It’s supports adding, modifying and removing fields, methods, annotations and values in enum out of the box.
Configuring Spring Loaded with Spring Boot and Gradle in IntelliJ
First of all, we need to setup
Spring Loaded as build script for Gradle.
Second, we need enable
idea plugin and change default output directory for compiled classes for IntelliJ.
If you don’t do that, Gradle and IntelliJ will compile classes into a different locations, so it will cause Spring Loaded to fail.
Also be sure that IntelliJ and Gradle use the same Java versions for compiling classes. Setup Java SDK same as Gradle in IntelliJ project structure, otherwise you get a exception when Spring Loaded try to reload a new compiled classes.
build.gradle should be similar to:
// add spring-loaded dependency
apply plugin: 'java'
// apply idea plugin
apply plugin: 'idea'
apply plugin: 'spring-boot'
// change default IntelliJ output directory for compiling classes
inheritOutputDirs = false
outputDir = file("$buildDir/classes/main/")
Spring Boot application from
IntelliJ IDE, we have to build project or compile single class file after change.
We can press shortcut for
Make Project and after build finishes, classes will gets reloaded by Spring Loaded.
Another option is to record a macro for
Save All and
Make Project ( if you have disabled option for auto save all changes in modified files ) and map it to one shortcut, eg.
Ctrl + S.
IntelliJ has also a feature called
Make Project Automatically. If we enable this option, IntelliJ will compiles code automatically after few seconds.
Unfortunately, this feature only works when application is not running / debugging inside the IDE.
To make this works together, we must start the application outside the IDE. Example project can be found here.
So, if you want reloading system in running Spring Boot application that is not limited to HotSwap mechanism for free, then give Spring Loaded a try. Otherwise you may also check JRebel ( not free ). In latest version 6.0, support for Spring Boot was added. JRebel also supports almost all major frameworks and application servers in Java ecosystem.