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 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.
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.
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.
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
.
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.
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.
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.
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.
Enabling and configuring DevTools in Spring Boot:
spring-boot-devtools
dependency in your project.build.gradle
):dependencies { implementation 'org.springframework.boot:spring-boot-devtools' // Other dependencies }
Hot swapping of code with DevTools in Spring Boot:
application.properties
:spring.devtools.restart.additional-exclude=static/**,public/**
Customizing property defaults with Spring Boot DevTools:
application.properties
or application.yml
.application.properties
):spring.devtools.restart.enabled=false
Disabling and fine-tuning DevTools features in Spring Boot:
application.properties
):spring.devtools.livereload.enabled=false
Remote development with Spring Boot DevTools:
application.properties
:spring.devtools.remote.secret=mysecret
Live reload and browser synchronization in Spring Boot:
application.properties
:spring.devtools.livereload.enabled=true