Spring Boot
IntelliJ IDEA
Auto Reload
Java Development
Developer Tools

how to make auto reload with Spring-boot on IDEA Intellij

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Developing Spring Boot applications with automatic reloading capabilities can significantly improve developer productivity and the feedback loop. Auto reload allows you to see the effects of code changes without manually restarting the application. In this article, we'll explore how to set up auto reload in a Spring Boot project using IntelliJ IDEA, leveraging tools like Spring DevTools and LiveReload.

Prerequisites

Before we proceed, ensure you have the following tools installed:

  • IntelliJ IDEA (Community or Ultimate)
  • Java Development Kit (JDK) 8 or higher
  • Apache Maven or Gradle
  • Basic understanding of Spring Boot

Step-by-Step Guide

Step 1: Set Up a Spring Boot Project

  1. Create a New Project: Open IntelliJ IDEA and create a new Spring Boot project. You can either start from scratch or import an existing project.
  2. Add Spring Initializr:
    Utilize Spring Initializr within IntelliJ IDEA or from start.spring.io to bootstrap your project. Ensure to include dependencies like Spring DevTools.

Step 2: Add Spring Boot DevTools

Spring Boot DevTools supports automatic restarts and speeding up the development cycle.

  1. Modify pom.xml or build.gradle:
    For Maven, add the following dependency in your pom.xml:
xml
1   <dependency>
2       <groupId>org.springframework.boot</groupId>
3       <artifactId>spring-boot-devtools</artifactId>
4       <optional>true</optional>
5   </dependency>

For Gradle, add the following line in build.gradle:

groovy
   implementation 'org.springframework.boot:spring-boot-devtools'
  1. Reimport the Project: After updating the build file, re-import the project in IntelliJ IDEA to ensure all dependencies are correctly downloaded.

Step 3: Enable Automatic Restart

Sprint Boot DevTools enhances the development experience by enabling automatic restarts.

  1. Configure Automatic Restart: By default, after adding Spring Boot DevTools, every code change triggers an automatic restart. You don't need to do additional configuration.
  2. Exclude Resources: If certain files should not trigger a restart, exclude them by modifying src/main/resources/META-INF/spring-devtools.properties:
properties
   restart.exclude.myprops=static/**/*,*/some-file.json

Step 4: LiveReload for Browser Refresh

LiveReload automatically refreshes the browser whenever a resource is updated.

  1. Configure LiveReload: LiveReload is included in Spring Boot DevTools. To use it, download a browser extension compatible with LiveReload or configure your IDE plugin.
  2. Build for LiveReload:
    • Ensure your application is running and you've configured the browser or IDE to support LiveReload.
    • Upon changes, Spring Boot DevTools will trigger a context restart, and LiveReload will refresh your browser.

Step 5: IntelliJ IDEA Configuration

  1. Enable Build Project Automatically:
    • Go to File > Settings (or IntelliJ IDEA > Preferences on macOS).
    • Navigate to Build, Execution, Deployment > Compiler.
    • Check the Build project automatically option.
  2. Make on Save:
    • Help the IDE in automatically building the project when files are saved.
    • Navigate to Preferences > Advanced Settings.
    • Under Compiler, enable Auto-make if the project is dirty option.

Conclusion

Setting up auto reload in Spring Boot projects using IntelliJ IDEA can greatly improve workflow efficiency by reducing manual restarts. By leveraging Spring Boot DevTools and LiveReload, developers can keep a tight feedback loop, see their changes in real-time, and focus more on writing code.

Key Points Summary

FeatureTool/ConfigurationDescription
Spring Boot Project SetupSpring InitializrBootstrap project with required dependencies
DevTools Setuppom.xml/build.gradleAdds capabilities for automatic restarts
Automatic RestartSpring Boot DevToolsDefault feature; No additional setup needed
Excluding Resourcesspring-devtools.propertiesDefine resources to avoid trigger on changes
Browser Auto-RefreshLiveReloadAutomatically refreshes the browser upon changes
IntelliJ ConfigurationBuild Project Automatically and Make on SaveOptimize the auto-build process

With this setup in place, you can enjoy a smoother development experience, focusing more on application logic and less on managing your development environment.


Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.