Introduction to Spring-Boot Dev Tools

Spread the love

1. Introduction

Spring Boot gives us the ability to quickly setup and run services. To enhance the development experience further, Spring released the spring-boot-devtools tool – as part of Spring Boot-1.3. This article will try to cover the benefits we can achieve using the new functionality.

We’ll cover the following topics:

  • Property defaults
  • Automatic Restart
  • Live Reload
  • Global settings
  • Remote applications

1.1. Add Spring-Boot-Devtools in a Project

Adding spring-boot-devtools in a project is as simple as adding any other spring-boot module. In an existing spring-boot project, add the following dependency:

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

Do a clean build of the project, and you are now integrated with spring-boot-devtools. The newest version can be fetched from here and all versions can be found here.

2. Property Defaults

Spring-boot does a lot of auto-configurations, including enabling caching by default to improve performance. One such example is the caching of templates used by template engines, e.g. thymeleaf. But during development, it’s more important to see the changes as quickly as possible.

The default behavior of caching can be disabled for thymeleaf using the property spring.thymeleaf.cache=false in the application.properties file. We do not need to do this manually, introducing this spring-boot-devtools does this automatically for us.

3. Automatic Restart

In a typical application development environment, a developer would make some changes, build the project and deploy/start the application for new changes to take effect, or else try to leverage JRebel, etc.

Using spring-boot-devtools, this process is also automated. Whenever files change in the classpath, applications using spring-boot-devtools will cause the application to restart. The benefit of this feature is the time required to verify the changes made is considerably reduced:

4. Live Reload

spring-boot-devtools module includes an embedded LiveReload server that is used to trigger a browser refresh when a resource is changed.

For this to happen in the browser we need to install the LiveReload plugin one such implementation is Remote Live Reload for Chrome.

5. Global Settings

spring-boot-devtools provides a way to configure global settings that are not coupled with any application. This file is named as .spring-boot-devtools.properties and is located at $HOME.

6. Remote Applications

6.1. Remote Debugging via HTTP (Remote Debug Tunnel)

spring-boot-devtools provides out of the box remote debugging capabilities via HTTP, to have this feature it is required that spring-boot-devtools are packaged as part of the application. This can be achieved by disabling excludeDevtools configuration in the plugin in maven.

Here’s a quick sample:

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

Now for remote debugging via HTTP to work, following steps have to be taken:

  1. An application being deployed and started on the server, should be started with Remote Debugging enabled:
-Xdebug -Xrunjdwp:server=y,transport=dt_socket,suspend=n
  1. As we can see, the remote debugging port is not mentioned here. Hence, java will choose a random port
  2. For the same project, open the Launch configurations, choose the following options:
    Select main class: org.springframework.boot.devtools.RemoteSpringApplication
    In program arguments, add the URL for the application, e.g. http://localhost:8080
  3. The default port for debugger via spring-boot application is 8000 and can be overridden via:
spring.devtools.remote.debug.local-port=8010

4. Now create a remote-debug configuration, setting up the port as 8010 as configured via properties or 8000, if sticking to defaults

6.2. Remote Update

The remote client monitors the application classpath for changes as is done for remote restart feature. Any change in the classpath causes, the updated resource to be pushed to the remote application and a restart is triggered.

Changes are pushed when the remote client is up and running, as monitoring for changed files is only possible then.

7. Conclusion

With this quick article, we have just demonstrated how we can leverage the spring-boot-devtools module to make the developer experience better and reduce the development time by automating a lot of activities.

Leave a Reply