What does Spring Boot Devtools (Development Tools)?

To make developers life easy, Spring Boot includes an additional set of tools that can help reduce development time and have some pleasant time in programming. The spring-boot-devtools module can be included in any project to provide additional development-time features. To include spring boot devtools support, add the module dependency to your build, as shown for Maven.

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

Developer tools are automatically disabled when running a fully packaged application. If your application is launched from java -jar or if it is started from a special class-loader, then it is considered a “production application”.

Otherwise excluding devtools can be done by setting -Dspring.devtools.restart.enabled=false in system property.

How to Enable Devtools in Spring Boot?

Devtools can be enabled either from application.properties or by using  DevToolsPropertyDefaultsPostProcessor.

If you don’t want property defaults to be applied you can set spring.devtools.add-properties to false in your application.properties.

For a complete list of the properties that are applied by the spring boot devtools, see DevToolsPropertyDefaultsPostProcessor.

Spring Boot Devtools Usages

1. Use in Cache Option

Spring Boot uses caches in several libraries to improve performance. For example, devtools avoid repeatedly parsing and compiling template files from template engines. Also, Spring MVC can add HTTP caching headers to responses when serving static resources.

While caching is very beneficial in production, it can be counter-productive during development, it stops reflecting the changes you just made in your application. For this reason, spring boot devtools disables the caching options by default.

 

2. Use in Automatic Restart

Applications that use spring-boot-devtools automatically restart whenever files on the classpath change. This can be a useful feature when working in an IDE, as it gives a very fast reflection on code changes. By default, any entry on the classpath that points to a folder is monitored for changes. Note that certain resources, such as static assets and view templates, do not need to restart the application.

You may want your application to be restarted or reloaded when you make changes to files that are not on the classpath. To do so, use thespring.devtools.restart.additional-paths property to configure additional paths to watch for changes.

Disabling Restart

If you do not want to use the restart feature, you can disable it by using the spring.devtools.restart.enabled property. In most cases, you can set this property in your application.properties (doing so still initializes the restart classloader, but it does not watch for file changes).

If you need to completely disable restart support (for example, because it does not work with a specific library), you need to set the spring.devtools.restart.enabled System property to false before calling SpringApplication.run(…​), as shown in the following example:

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

3. Use in Remote Application

The Spring Boot developer tools are not limited to local development. You can also use several features when running applications remotely. To enable it, you need to make sure that devtools is included in the repackaged archive, as shown below,

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

Then you need to set a spring.devtools.remote.secret property, as shown in the following example:

spring.devtools.remote.secret=mysecret

Enabling spring-boot-devtools on a remote application is a security risk. You should never enable support on a production deployment.

Remote devtools support is provided in two parts: a server-side endpoint that accepts connections and a client application that you run in your IDE. The server component is automatically enabled when the spring.devtools.remote.secret property is set. The client component must be launched manually.

Conclusion

Developer tools in spring boot is can be used efficiently in different perspective as described above. Hence we saw usage of spring boot devtools and enabling it in different levels.

Please follow and like us:
error