How to restart your Spring Boot application automatically for every code change?

One factor which slows down developer’s productivity is restarting the application for every code change to reflect. And even start up time takes time. The time is much less in Spring Boot compared to traditional MVC applications. And new frameworks like Micronaut even have faster start up times.

To avoid the manual restart , Spring Boot provides a dependency which when included in your pom.xml automatically restarts the application every time you make a change.

Below is the maven dependency to be included if you are using Maven:

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

There is no other change required. Spring Boot automatically restarts app for every code change . This dependency is not included as part of the final build package so developers don’t need to worry about production servers automatically getting restarted for every code change.

The above restart happens only if any file included in the classpath changes.

What if you want to trigger a restart when an external file is changed?

Add the below property in your application.properties/yml file:

spring.devtools.restart.additional-paths=

What if you want to trigger a restart only after a particular file is changed?

You may want to trigger a restart only after you are done up with all your changes . And for this to happen you may decide to trigger a restart only when a particular file is changed. You can change that file at the end of your current coding session and it will trigger a restart . To enable this configure the file name in application.properties as below:

spring.devtools.restart.trigger-file= 

What if you want to refresh the browser as well every time your application is restarted?

Install livereload browser extension from http://livereload.com/extensions/ (suggested by Spring)

Once installed you will see an icon in the browser like this (I am using Chrome):

Click on the icon to enable live reload:

No other changes required!

Spring Boot automatically starts a LiveReload server . You can see it on startup:

2020-06-16 13:04:27.363 INFO 22936 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729

And then browser gets refreshed automatically on every restart.

Spring Dev Tools provides a few features to increase developer productivity , the above some among them.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s