Learnitweb

Spring Boot DevTools

1. Introduction to DevTools

Spring Boot became popular as it made easier to create production grade application and run. Spring Boot provides additional set of tools which help in development. These tools are provided in spring-boot-devtools module. When included in the project, it provides extra features to the developers. Following are the Maven and Gradle dependencies:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>
dependencies {
    developmentOnly("org.springframework.boot:spring-boot-devtools")
}

In this tutorial, we’ll discuss following with respect to developer tools:

  • Property Defaults
  • Automatic Restart
  • LiveReload
  • Global Settings
  • Remote Applications

2. Property Defaults

Several libraries use cache to improve performance. The cached values are returned which is faster in terms of performance. For example, Spring MVC can add HTTP caching headers while serving static content. However, sometimes it is not desired to get cached values as we may not see latest values.

spring-boot-devtools disables the caching options by default.

You can set spring.devtools.add-properties to false in application.properties, if you don’t want to apply property defaults.

3. Automatic Restart

In Angular application, if we make any change in file, the changes are reflected in browser. This is a very cool feature to have as it helps developers to check output after every change. Automatic restart and LiveReload are provided by spring-boot-devtools.

If an application uses spring-boot-devtools, it restarts whenever a file in classpath is changed.

Following are few important points to note:

  • DevTools uses application context’s shutdown hook to close it during a restart. It will not work properly if you disable the shutdown hook. Shutdown hook can be disabled by the following
    SpringApplication.setRegisterShutdownHook(false);
  • Automatic restart is not supported when using AspectJ weaving.

3.1 How restart by DevTools in Spring Boot works?

Two classloaders are used for this purpose, base classloader and restart classloader. Classes that do not change are loaded in base classloader. For example, classes from third-party jars are loaded in base classloader. Classes which are used in development and changing are loaded into restart classloader. When application is restarted, restart classloader is discarded and a new one is created. Since base classloader is already available, the restart in Spring Boot are faster.

3.2 Condition evaluation delta

The report includes information like added beans, removed beans, change in configuration properties etc. The logging of this report can be disabled by setting the following property:

spring.devtools.restart.log-condition-evaluation-delta=false

3.3 Excluding resources

Sometimes there is no need to restart application when certain type of files like changed, like HTML files and template files. By default, change of file in following folders does not trigger a restart of application:

  • /META-INF/maven
  • /META-INF/resources
  • /resources
  • /static
  • /public
  • /templates

The change of file in any of the above location triggers a live reload. Live reload is trigger of browser refresh when a resource is changed. We can modify this default list by setting spring.devtools.restart.exclude property. For example, to exclude only /public and /static, set the following:

spring.devtools.restart.exclude=public/**,static/**

If you want to keep the default values and add more locations to the default list, the property to use is spring.devtools.restart.additional-exclude instead of spring.devtools.restart.exclude.

3.4 Watch additional paths

As discussed earlier, application restart is triggered when file in classpath is changed. Sometimes, we want to watch additional paths to trigger application restart. We can use spring.devtools.restart.additional-paths property to watch additional paths for changes.

3.5 Disable application restart

Sometimes we may need to disable the restart support. We need to set spring.devtools.restart.enabled System property to false before calling SpringApplication.run(…​).

@SpringBootApplication
public class DisableRestartDemo {

    public static void main(String[] args) {
        System.setProperty("spring.devtools.restart.enabled", "false");
        SpringApplication.run(DisableRestartDemo.class, args);
    }
}

3.6 Using a Trigger File to trigger application restart

Sometimes you are changing many files during development. Your IDE may continuously compile files as and when you save a change. This could be problematic sometimes if restarts takes time. One option is to check for a particular file for change. This special file is the trigger file which will trigger application restart when modified.

To use a trigger file set the spring.devtools.restart.trigger-file property. The value of the property is the name of the file without the path. The file needs to be in the classpath.

spring.devtools.restart.trigger-file=.triggerFile

Here, .triggerFile is the name of the file to be watched for modification.

3.7 Customize Restart Classloader

As discussed earlier, DevTools use base classloader and restart classloader. We can customize how class files are loaded in the base and restart classloader. To do this create file META-INF/spring-devtools.properties.

restart.exclude.commonlibs=/learnitweb-common-[\\w\\d-\\.]+\\.jar
restart.include.globallibs=/learnitweb-myproj-[\\w\\d-\\.]+\\.jar

4. LiveReload

LiveReload is the trigger of browser refresh when a resource is changed. The spring-boot-devtools module includes an embedded LiveReload server. Browser extensions for LiveReload are freely available at livereload.com.

If you do not want to start the LiveReload server when application restarts, set the following following property:

spring.devtools.livereload.enabled property=false

5. Global Settings for DevTools

You can configure global devtools settings by adding any of the files to the $HOME/.config/spring-boot directory:

  • spring-boot-devtools.properties
  • spring-boot-devtools.yaml
  • spring-boot-devtools.yml

Properties added to these files apply to all Spring Boot applications on your machine that use devtools.

Few important points to note:

  • If configuration files are not found in $HOME/.config/spring-boot, the root of the $HOME directory is searched for the presence of a .spring-boot-devtools.properties file.
  • Profiles are not supported in devtools properties and yaml files.

Configure File System Watcher

Two properties spring.devtools.restart.poll-interval and spring.devtools.restart.quiet-period are used by FileSystemWatcher. First property defines the time interval after which the classpath directories are monitored. Second property defines the quiet period to be maintained to make sure there are no additional class changes.

6. Remote Applications

DevTools provide several features for running remote applications. For having this support, two things are required:

  1. devTools is included in repacakged archive.
  2. spring.devtools.remote.secret property is set.

To make sure that the DevTools is included in repacakged archive, set excludeDevtools to false like the following:

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

The remote support of DevTools works on client-server architecture. Server endpoint is enabled when the spring.devtools.remote.secret property is set. The client application in your IDE is required to run manually.

To run the application in IDE, create a run configuration. Use org.springframework.boot.devtools.RemoteSpringApplication as the main class and add the remote URL to the arguments.

Conclusion

In this tutorial, we discussed DevTools and the support it provides for development. Every developer should know about above discussed points as it is really helpful and makes the life of a developer easier.