Spring Boot Dev Tools

Introduction to Spring Boot Dev Tools

Spring Boot comes with a lot of features and one of such feature is to help in developer productivity. In this post, we will cover about Spring Boot Dev Tools.

 

Introduction

One of the main advantages of using Spring Boot is its production ready features, to provide these features, Spring Boot use certain pre-defined configurations.

  • If you use Thymeleaf for your application, it’s caching is enabled by default until we switch it off.
  • We need quick deployment of code changes with a minimal deployment and server restart time.

These features are good but can slow down development with frequent changes in our code and want to see changes immediately. We have the option to use 3rd party tools like Jrebel to help in this but these tools are not free and need a significant amount to get a license (Jrebel is really a great tool and if you can get it, I will highly recommend it). Spring Boot 1.3 introduced Spring Boot Dev Tools module, aimed to help developers in improving the productivity. We will cover following features of the Spring Boot Dev Tool

  • What is Spring Boot Dev Tools
  • What are Property Defaults
  • Live Reload
  • Automatic Restart
  • Remote Debugging

 

What is Spring Boot Dev Tools?

They introduced spring Boot Dev Tools module in 1.3 to provide a powerful tool for the development. It helps developers to shorten the development cycle and enable easy deployment and testing during the development. To add use the feature, we need to add a spring-boot-devtools dependency in our build. We need to add the following dependency to our Maven POM

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

if you are using Gradle as your build tool

dependencies {
    compile("org.springframework.boot:spring-boot-devtools")
}

Once we perform build, it will add a spring-boot-devtools to our project with its developer-friendly features. Let’s explore these features.

 

2. Property Defaults

Spring Boot comes with many productions-ready features (Also known as auto-configuration) which include caching for its modules for performance. To boost performance, template engines might cache all compiled templates to avoid template parsing on each request. This is helpful once we deploy our application on production but can be problematic during the development (We might not see changes immediately).

e.g. If you are using Thymeleaf with Spring Boot, you might be familiar that Spring Boot automatically cache it. You can always control this by setting spring.thymeleaf.cache to false under application.properties.With spring-boot-devtools, you need not change this property manually but this module will do it automatically for you.

 

3. Automatic Restart

Typically, as a development life-cycle, we change our code, deploy it and test it and if things are not working as expected we will repeat this cycle. We can always use third-party tools like Jrebel to help in this. Spring Boot Dev Tools provide a similar feature (not as quick as Jrebel) to auto restart. Whenever a file changes in the classpath, spring-boot-devtools module will restart application automatically.

When you start your application with dev tools, you will find similar logs on the startup.

19:20:19.637 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Included patterns for restart : []
19:20:19.655 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Excluded patterns for restart : [/spring-boot-starter/target/classes/, /spring-boot-autoconfigure/target/classes/, /spring-boot-starter-[\w-]+/, /spring-boot/target/classes/, /spring-boot-actuator/target/classes/, /spring-boot-devtools/target/classes/]
19:20:19.656 [main] DEBUG org.springframework.boot.devtools.restart.ChangeableUrls - Matching URLs for reloading : [file:/Users/target/classes/]

change your application code and perform build, it will trigger an automatic restart. Here are the logs from the restart

2017-12-18 19:25:11.111  INFO 31623 --- [      Thread-28] ationConfigEmbeddedWebApplicationContext : Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7bc4b8cd: startup date [Mon Dec 18 19:20:20 PST 2017]; root of context hierarchy
2017-12-18 19:25:11.197  INFO 31623 --- [      Thread-28] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown

Spring Boot use 2 class loader internally to handle this restart. we will cover this feature in another post.

 

3.1 Exclusion

For certain resources, we really don’t need any restart (Think of change in static resources and templates), spring-boot-devtools will not trigger restart if you change resources at /META-INF/ resources, /resources, /static, /public, /template, if you want you can customize this behavior using spring.devtools.restart.exclude property.

 

3.2 Disable Restart

If you want to use the spring-boot-devtools module but like to disable restart feature, you can easily customize it by setting spring.devtools.restart.enabled in your application.properties file, you can disable this feature by setting this on the System property.

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

 

4. Live Reload

The dev tools come with an embedded LiveReload server which will automatically trigger browser refresh when resource change. Visit  livereload.com for more information.

 

5. Remote Debugging via HTTP

Spring Boot dev tools provide ready to use remote debugging capabilities, to use this feature on the remote application, we have to make sure that devtools in included in the deployment packet. We can achieve this by setting the excludeDevtools property in our POM.xml file 

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

 

To allow client to allow remote debugging, we need to make sure following steps

  • Start our application (Deployed on the server) with remote debugging enabled. This can be done by starting application with this additional parameters -Xdebug -Xrunjdwp:server=y,transport=dt_socket,suspend=n
  • System will automatically pick the free port
  • Open Launch configurations with RemoteSpringApplication.
  • Default debug port for the system using spring-boot is 8080.

[pullquote align=”normal”]For debugging an application using IntelliJ. Please read Remote debug spring boot application with the maven and IntelliJ [/pullquote]

 

6. Remote Update

Spring Boot development tool also support update and restart for remote application. The remote client will monitor changes in the local classpath and will trigger a restart after pushing these changes to the remote server. This can be a handy feature if your work involves a cloud service.

 

6. Global Setting

The last feature of spring-boot-devtools is the ability to provide a global configuration which is not specific to any application. This can be achieved by adding a file named .spring-boot-devtools.properties in the $HOME. These configurations will apply to all the Spring Boot application on our machine.

 

Summary 

Spring Boot Dev Tools comes with many built-in features to help in the development life cycle and make development experience better. We learned how to enable by using these features provided under spring-boot-devtools modules.

Comments are closed.

Scroll to Top