Skip to content

Instantly share code, notes, and snippets.

@IMRFeng
Last active June 4, 2024 04:08
Show Gist options
  • Save IMRFeng/eed589de6a6362ef23bc189fb135fdea to your computer and use it in GitHub Desktop.
Save IMRFeng/eed589de6a6362ef23bc189fb135fdea to your computer and use it in GitHub Desktop.
How to add Spring Boot Devtools into the project to enhance your development experience

Spring boot devtools has been shipped since 1.3.0 of Spring Boot release, it's a really handy and productive tool/module that can help you speed up development time without rely on or configure any other third-party libraries like JRebel, or SpringLoad.

Here I'll just talking about 3 of features of the Spring boot. Download Demo Code.

First and foremost, adding spring-boot-devtools into the project, the followings are two ways to add them via Maven and Gradle respectivily:

  • Maven

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

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

    When it comes to instant reload for your java application, you may have used tools like Spring Loaded or JRebel. They are great but you have to set up additional configuration or IDE plugins to work. However, with the spring-boot-devtools module included, any classpath file changes will automatically trigger an application restart. We do some tricks to try and keep restarts fast, so for many microservice style applications this technique might be good enough.

    Triggering a restart

    As DevTools monitors classpath resources, the only way to trigger a restart is to update the classpath. The way in which you cause the classpath to be updated depends on the IDE that you are using. In Eclipse, saving a modified file will cause the classpath to be updated and trigger a restart. In IntelliJ IDEA, building the project (Build → Build Project) will have the same effect.

  • LiveReload

    It's a little tedious that you have to manually click the browser refresh button each time when a resource is changed. Spring Boot DevTools, however, includes an embedded LiveReload server. LiveReload is a simple protocol that allows your application to automatically trigger a browser refresh whenever things change. Browser extensions are freely available for Chrome, Firefox and Safari from livereload.com.

    You can only run one LiveReload server at a time. Before starting your application, ensure that no other LiveReload servers are running. If you start multiple applications from your IDE, only the first will have LiveReload support.

  • Remote update and restart

    DevTools support for remote application updates and restarts. This works by monitoring your local classpath for file changes and pushing them to a remote server which is then restarted. As with local restarts, you can also use this feature in combination with LiveReload.

    To enable it you need to make sure that devtools is included in the repackaged archive:

     <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, for example:

    spring.devtools.remote.secret=neon-secret

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

    Then open the Launch confconfigurations, choose the following options:

    • Select main class: org.springframework.boot.devtools.RemoteSpringApplication
    • Program arguments: add the URL of the application, e.g. http://localhost:8080
  • References

    1. http://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-devtools.html#using-boot-devtools

    2. https://patrickgrimard.io/2016/01/18/spring-boot-devtools-first-look/

      Spring Developer Tools

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment