Spring Boot Tutorial

Spring Boot - Software Setup and Configuration (STS/Eclipse/IntelliJ)

Prerequisite (Spring Core Concepts)

Spring Boot Core

Spring Boot with REST API

Spring Boot with Database and Data JPA

Spring Boot with Kafka

Spring Boot with AOP

Spring Boot - DevTools

Spring Boot DevTools is a developer-friendly tool that aims to enhance the development experience by simplifying certain tasks and automating others. It provides features like automatic restart, live reload, and property overrides which are invaluable during the development phase of a project.

Key Features of Spring Boot DevTools:

  1. Automatic Restart: Whenever files are changed in the classpath, the application restarts. This restart is faster than a full restart because DevTools uses two classloaders. Classes that don't change (e.g., libraries) are loaded into a base classloader, while your project classes are loaded into another. Only the project's classloader restarts, which makes it quicker.

  2. LiveReload: Spring Boot DevTools includes a LiveReload server that can trigger a browser refresh when resources are changed. This can be especially useful when working on web interfaces. You can also use browser extensions to integrate with the LiveReload server.

  3. Property Overrides: You can store certain properties in ~/.spring-boot-devtools.properties (your home directory) and they'll be automatically applied to any Spring Boot application on your machine. This can be useful for setting up a particular environment configuration without affecting the project's application.properties.

  4. Exclusion of Static Resources: By default, changes to static resources in src/main/resources trigger a restart. But DevTools automatically excludes these from causing a full restart.

  5. Remote Applications: DevTools can be used with applications running remotely. With remote support enabled, you can connect to a remote application and, for instance, use the local browser to debug an application running in a different environment.

How to Add DevTools to Your Project:

For a Maven project, add the following dependency to the pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-devtools</artifactId>
    <scope>runtime</scope>
    <optional>true</optional>
</dependency>

For a Gradle project, add the following to your build.gradle:

runtimeOnly("org.springframework.boot:spring-boot-starter-devtools")

Note: Setting optional to true in Maven or using runtimeOnly in Gradle ensures that DevTools isn't included in the production build, which is a recommended best practice.

Disabling Specific Features:

Some DevTools features can be disabled in the application.properties:

# To disable restart
spring.devtools.restart.enabled=false

# To disable the LiveReload server
spring.devtools.livereload.enabled=false

Remember, while DevTools is fantastic for development, it's crucial not to deploy it in production. As of Spring Boot 2.3, the library is automatically disabled when identified as a built artifact (like a JAR or a WAR), but it's always a good idea to double-check and follow best practices.

  1. Enabling and configuring DevTools in Spring Boot:

    • Include the spring-boot-devtools dependency in your project.
    • Example (build.gradle):
      dependencies {
          implementation 'org.springframework.boot:spring-boot-devtools'
          // Other dependencies
      }
      
  2. Hot swapping of code with DevTools in Spring Boot:

    • DevTools supports hot swapping, allowing you to see changes without a full application restart.
    • Enable hot swapping in application.properties:
      spring.devtools.restart.additional-exclude=static/**,public/**
      
  3. Customizing property defaults with Spring Boot DevTools:

    • Customize DevTools settings using application.properties or application.yml.
    • Example (application.properties):
      spring.devtools.restart.enabled=false
      
  4. Disabling and fine-tuning DevTools features in Spring Boot:

    • Disable specific DevTools features or configure their behavior.
    • Example (application.properties):
      spring.devtools.livereload.enabled=false
      
  5. Remote development with Spring Boot DevTools:

    • Use DevTools remotely by connecting to a running application.
    • Enable remote support in application.properties:
      spring.devtools.remote.secret=mysecret
      
  6. Live reload and browser synchronization in Spring Boot:

    • DevTools includes LiveReload for automatic browser synchronization.
    • Enable LiveReload in application.properties:
      spring.devtools.livereload.enabled=true