67. Hot swapping

67.1 Reload static content

There are several options for hot reloading. Running in an IDE (especially with debugging on) is a good way to do development (all modern IDEs allow reloading of static resources and usually also hot-swapping of Java class changes). The Maven and Gradle plugins also support running from the command line with reloading of static files. You can use that with an external css/js compiler process if you are writing that code with higher level tools.

67.2 Reload Thymeleaf templates without restarting the container

If you are using Thymeleaf, then set spring.thymeleaf.cache to false. See ThymeleafAutoConfiguration for other Thymeleaf customization options.

67.3 Reload FreeMarker templates without restarting the container

If you are using FreeMarker, then set spring.freemarker.cache to false. See FreeMarkerAutoConfiguration for other FreeMarker customization options.

67.4 Reload Groovy templates without restarting the container

If you are using Groovy templates, then set spring.groovy.template.cache to false. See GroovyTemplateAutoConfiguration for other Groovy customization options.

67.5 Reload Velocity templates without restarting the container

If you are using Velocity, then set spring.velocity.cache to false. See VelocityAutoConfiguration for other Velocity customization options.

67.6 Reload Java classes without restarting the container

Modern IDEs (Eclipse, IDEA, etc.) all support hot swapping of bytecode, so if you make a change that doesn’t affect class or method signatures it should reload cleanly with no side effects.

Spring Loaded goes a little further in that it can reload class definitions with changes in the method signatures. With some customization it can force an ApplicationContext to refresh itself (but there is no general mechanism to ensure that would be safe for a running application anyway, so it would only ever be a development time trick probably).

67.6.1 Configuring Spring Loaded for use with Gradle and IntelliJ

You need to jump through a few hoops if you want to use Spring Loaded in combination with Gradle and IntelliJ. By default, IntelliJ will compile classes into a different location than Gradle, causing Spring Loaded monitoring to fail.

To configure IntelliJ correctly you can use the idea Gradle plugin:

buildscript {
    repositories { mavenCentral() }
    dependencies {
        classpath "org.springframework.boot:spring-boot-gradle-plugin:1.1.8.RELEASE"
        classpath 'org.springframework:springloaded:1.2.0.RELEASE'
    }
}

apply plugin: 'idea'

idea {
    module {
        inheritOutputDirs = false
        outputDir = file("$buildDir/classes/main/")
    }
}

// ...
[Note]Note

Intellij must be configured to use the same Java version as the command line Gradle task and springloaded must be included as a buildscript dependency.

You can also additionally enable ‘Make Project Automatically’ inside Intellij to automatically compile your code whenever a file is saved.