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 be covering about Spring Boot Dev Tools.

 

Introduction

One of the main advantages of using Spring Boot is it’s production ready features, in order to provide these features, Spring Boot use certain predefined configurations.

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

These features are good but can slow down development when we will be making 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 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 which was aimed to help developers in improving the productivity.  We will be covering 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 Dev Tools?

Spring Boot Dev Tools module was introduced 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. In order to add use 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, spring-boot-devtools will be added to our project with its developer-friendly features. Let’s start exploring these features.

 

2. Property Defaults

Spring Boot comes with many production-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 really 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 with 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 don’t need to change this property manually but this module will do it automatically for you.

 

3. Automatic Restart

Typically as a development lifecycle, we make changes to our code, deploy it and test it and if things are not working as expected we will repeat this cycle. We can always use some third party tools like Jrebel to help in this (Jrebel check for the file change in the classpath ).

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/]

make some changes to your application code and perform build, an automatic restart will be triggered by spring-boot-devtools. 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 be covering 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 completely 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

Spring Boot 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, in order 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>

 

In order 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 (We have not given any port in our parameters)
  • Open Launch configurations with RemoteSpringApplication.
  • Default debug port for the system using spring-boot is 8080.
  • For debugging an application using IntelliJ. Please read Remote debug spring boot application with maven and IntelliJ

 

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 be applicable 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 lifecycle and make development experience better. We learned how to enabled by using these features provided under spring-boot-devtools modules.

Umesh

Hello!! I am Umesh- an engineer by profession and a photographer by passion.I like to build stuff on the web using OSS and love to capture the world through my lens.

follow me on:

1
Leave a Reply

avatar
0 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
0 Comment authors
Recent comment authors

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
newest oldest most voted
Notify of