How to use 5 important features of Springboot Devtools?

0
2632
5 most important features of springboot dev tools

In this article, we learn how to use Springboot dev tools. Spring Boot comes with a lot of features and one of those features is designed to help developer productivity. In this article, we’re going to discuss Spring Boot Dev Tools.

Springboot Devtool Introduction

One of the key benefits of using Spring Boot is its ready-made applications, to have these applications, Spring Boot uses certain pre-defined configurations.
⦁ If you use Thymeleaf for your application, caching is allowed by default before it is turned off.
⦁ We need fast execution of code changes with minimum deployment and server restart time.

These features are good, but they can slow down the development with regular updates to our code and want to make updates instantly. We have the option of using 3rd party software like Jrebel to help with this, but these software are not free and require a large amount to get a license (Jrebel is such a fantastic tool and if you can get it, I’ll highly recommend it). Spring Boot 1.3 launched the Spring Boot Dev Tools module, which aims to help developers increase productivity. We’ll discuss the following aspects of the Spring Boot Dev Tool

⦁ What really is Spring Boot Dev Tools
⦁ What are the Property Defaults
⦁ Reload live
⦁ Restarting automatically
⦁ Debugging application Remotely

What really is SpringBoot Dev Tools

In 1.3, they introduced the SpringBoot Dev Tools module to provide a powerful tool for development. It allows developers to shorten the development cycle and encourages fast implementation and testing during development. To add the use of this functionality, we need to add a spring-boot-dev tools dependency to our build. We need to apply to our Maven POM the following dependency.

pom.xml

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

If you use Gradle as your tool to build

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

What are Property Defaults?


SpringBoot comes with several production-ready features (also known as auto-configuration) that include caching for its output modules. To boost output, template engines can hide all templates that have been compiled to avoid template parsing on each request. This is useful when we launch our production application but can be troublesome during development (we may not see improvements immediately).
Ok, e.g. If you are using Thymeleaf with Spring Boot, you might be aware that Spring Boot automatically caches it. You can still manage this by setting spring.thymeleaf.cache to false under application.properties. With spring-boot-devtools, you don’t need to adjust this property manually, but this module can do it automatically for you.

Reload live

Dev tools come with an integrated LiveReload server that will instantly update the window as the resource changes. Please visit livereload.com for more information.

Restarting automatically

Usually, as a life-cycle development, we update our code, deploy and validate it, and if things don’t perform as planned, we’ll restart this cycle. We can still use third-party software like Jrebel to assist with this. Spring Boot Dev Tools have a similar feature (not as fast as Jrebel) to auto restart. Whenever a file changes to the classpath, the spring-boot-dev tools module automatically restarts the program.
When you launch your application with dev tools, you can find identical logs on the startup.

[main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Included patterns for restart : []
[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/]
[main] DEBUG org.springframework.boot.devtools.restart.ChangeableUrls - Matching URLs for reloading : [file:/onurdesk/target/classes/]

Changing the application code and running the create process would cause an automatic restart. Here are the logs of the restart

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

Changing the application code and running the create process would cause an automatic restart. Here are the logs of the restart

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

Exclusion

With certain services, we don’t even need a reboot (think about modifying static services and templates), spring-boot-dev tools won’t reboot if you modify resources at / META-INF / sources,/resources,/static,/public,/template, if you want you can configure this behavior using the spring.devtools.restart.exclude property.

Restart Disabled

If you continue to use the springboot-devtools module but want to disable the restart feature, you can easily configure it by setting spring.devtools.restart.enabled in your application.properties file, you can disable this function by setting it to the System domain.

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

Debugging application Remotely

Spring Boot Dev Tools are ready to use remote debugging capabilities, to use this feature on a remote application, we need to make sure the devtools are included in the deployment package. We can do 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 the client to allow remote debugging, we need to ensure that the following steps are taken.
⦁ Start our application (Deployed to the server) with remote debugging allowed. This can be done by beginning the application with these additional parameters -Xdebug-Xrunjdwp: server = y, transport = dt socket, suspend = n
⦁ The system would automatically choose a free port
⦁ Open RemoteSpringApplication with Launch Configurations.
⦁ The default debug port for the spring-boot device is 8080.

Summary

SpringBoot Dev Tools comes with several built-in features to help with the life cycle of development and enhance development experience. We have learned how to enable using the functionality included in the spring-boot-devtools modules.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

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