Back to Master Spring Boot
    Topic 2

    Using Developer Tools

    Boost your development productivity with Spring Boot DevTools - automatic restarts, live reload, and development-time configurations

    Introduction

    Spring Boot DevTools is a development toolset designed to enhance the productivity of developers by providing features like automatic restart, live reload, and property overrides. It simplifies the process of testing and tweaking applications during development by automatically applying changes without requiring a manual restart.

    DevTools also provides helpful development-time features such as disabling caching and enabling detailed debugging, making it easier to iterate quickly and see results immediately. It's ideal for speeding up the feedback loop in local development environments.

    Automatic Restart

    Quick restart on code changes

    Live Reload

    Browser refreshes automatically

    Property Overrides

    Development-time configurations

    Installing DevTools

    Add DevTools Dependency:

    Make sure you have the spring-boot-devtools dependency in your pom.xml or build.gradle file.

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

    After adding the dependency, you need to configure your IDE settings to allow automatic restarting.

    IDE Settings (IntelliJ IDEA)

    Step 1: Enable Build Project Automatically

    In IntelliJ IDEA, you can enable the "Build project automatically" option by going to:

    Settings → Build, Execution, Deployment → Compiler → ✓ Build project automatically

    This allows the IDE to automatically compile your project whenever changes are made, which works well in conjunction with Spring Boot DevTools to trigger the automatic restart feature.

    Step 2: Enable Auto-make During Runtime

    Go to Advanced Settings and check whether the following option is enabled:

    Advanced Settings → ✓ Allow auto-make to start even if developed application is currently running

    If it's not enabled, enable it and then press OK.

    Implementation Example

    Let's see DevTools in action. Here's a DTO class with an 'id' attribute:

    Original DTO (with 'id'):

    ProductDto.java
    packagecom.example.user.product_ready_features.dtos;importlombok.AllArgsConstructor;importlombok.Data;importlombok.NoArgsConstructor;@Data@NoArgsConstructor@AllArgsConstructorpublicclassProductDto{privateLong id;privateString title;privateString description;}

    After running the application, we can send a request using Postman. Now, let's change the attribute from 'id' to 'productId' while the application is running:

    Modified DTO (with 'productId'):

    ProductDto.java (Modified)
    packagecom.example.user.product_ready_features.dtos;importlombok.AllArgsConstructor;importlombok.Data;importlombok.NoArgsConstructor;@Data@NoArgsConstructor@AllArgsConstructorpublicclassProductDto{privateLong productId;privateString title;privateString description;}

    What happens?

    • First request: Response still contains 'id' instead of 'productId'
    • Second request: Application automatically restarts, response now includes 'productId'

    Important!

    Ensure that spring.jpa.hibernate.ddl-auto=update is set in the application properties instead of create. If you set it to create, the productId will be null.

    Automatic Restart

    Automatic Restart is a feature of Spring Boot DevTools that enhances developer productivity by automatically restarting the application when it detects changes in the classpath. Instead of manually stopping and restarting the application after every code change, DevTools monitors the project files for updates (such as changes in classes or resources) and triggers a restart automatically.

    Key Aspects of Automatic Restart:

    It applies only to the classpath entries that belong to the project, meaning changes in external libraries won't trigger a restart.

    Restarts are faster than a full application restart because Spring Boot only reloads the affected parts of the application, reusing the already loaded classes.

    It helps maintain the application state in a development environment by allowing quick feedback from code changes.

    Useful Configurations for DevTools

    spring.devtools.restart.enabled=false

    Disables automatic restarts. Useful when you want to manually control restarts during development.

    spring.devtools.restart.enabled=true

    Enables the automatic restart feature. When set to true, the application will automatically restart when it detects changes in the classpath. This is particularly useful during development for quicker testing of changes.

    spring.devtools.restart.exclude=static/**,public/**

    Specifies patterns for files or directories that should be excluded from triggering a restart. This helps avoid unnecessary restarts for static resources.

    spring.devtools.restart.pollInterval=20

    Sets the interval (in milliseconds) for checking changes in the classpath. A shorter interval means faster detection of changes but may increase CPU usage.

    spring.devtools.restart.quietPeriod=10

    Defines a delay (in milliseconds) before a restart is triggered after changes are detected. This helps avoid multiple rapid restarts when making several changes in quick succession.

    Tip: Excluding Specific Packages

    To exclude a specific package: Navigate to the package, right-click → Copy Path/Reference → Copy the content root path and paste it:

    spring.devtools.restart.exclude=src/main/java/com/example/user/.../dtos/**

    Conclusion

    Spring Boot DevTools is a powerful toolset designed to boost developer productivity with features like automatic restart, live reload, and property overrides. It simplifies the development process by enabling seamless testing and iteration without manual restarts. Proper configuration ensures efficient workflows and quick feedback for code changes.

    Faster Development

    No manual restarts needed

    Instant Feedback

    See changes immediately

    Configurable

    Fine-tune restart behavior

    Easy Setup

    Just add a dependency

    💬 Comments & Discussion