Using Spring Boot DevTools for Faster Development and Live Reload

Spring Boot DevTools is a valuable tool for speeding up the development process by enabling features like automatic application restart and live reload of web content. It is specifically designed to improve the development experience by reducing the time required to restart the application during testing and debugging.

Here is a quick guide to using Spring Boot DevTools for faster development and live reload:


1. Add DevTools Dependency

To use Spring Boot DevTools, include it in your pom.xml (Maven) or build.gradle (Gradle) file.

Using Maven:

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

Using Gradle:

implementation 'org.springframework.boot:spring-boot-devtools'

2. Automatic Restart

Spring Boot DevTools triggers an automatic application restart whenever files in the classpath are modified. It uses two classloaders—one for the static resources and one for the application classes—enabling a fast reload experience.

  • Restart Triggering: Files located under /src/main/resources/, /src/main/java/, or any other classpath resources automatically restart the application upon modification.
  • Excluding Certain Files from Restart: You can exclude specific file patterns using the property:
spring.devtools.restart.exclude=static/**,public/**

3. Live Reload (Optional with Browser)

Spring Boot DevTools integrates with LiveReload, so front-end changes (e.g., HTML, CSS, or JavaScript) trigger an automatic browser reload.

Steps for LiveReload:

  1. Install a LiveReload extension in your browser (available for Chrome, Firefox, etc.).
  2. DevTools will automatically enable LiveReload if the extension is active.

If you want to disable the LiveReload capability, use the following property:

spring.devtools.livereload.enabled=false

4. Property Defaults in Development vs. Production

DevTools provides sensible defaults for development environments that are different from production. For instance:

  • Caching is disabled for templates (e.g., Thymeleaf, FreeMarker, etc.).
  • Hibernate auto-detection for changes in the database schema is enabled.

If you want to customize DevTools properties, you can use a dedicated application-dev.properties profile.

5. How to Enable Conditional DevTools Behavior

To avoid shipping DevTools to production, mark it as optional=true in Maven or use a developmentOnly configuration in Gradle.

Example for Gradle:

developmentOnly 'org.springframework.boot:spring-boot-devtools'

6. Disable Restart in Specific Scenarios

If you don’t want restart functionality during your development, you can disable it with the property:

spring.devtools.restart.enabled=false

7. Trigger a Manual Restart

If you want to trigger a restart manually during development, you can:

  • Add or remove files in the /META-INF/spring-devtools.properties directory to trigger a restart.

8. Example Use Case: Thymeleaf or Front-End Modification

If you’re using Thymeleaf templates in a Spring Boot application for the web frontend:

  • Modify an HTML file under /src/main/resources/templates.
  • The browser will refresh automatically (if LiveReload is enabled). You’ll instantly see your changes without manually restarting or refreshing.

Important Notes:

  • Spring Boot DevTools is solely for development purposes and should not be packaged into your production build.
  • If you’re running in Docker or a cloud environment, ensure that file watchers are properly configured for file changes.

Incorporating Spring Boot DevTools provides a more efficient development experience by automating repetitive actions and making live coding efforts more seamless!

How do I resolve circular dependencies in Spring beans?

Circular dependencies in Spring occur when two or more beans depend on each other, forming a cycle. For example, if BeanA depends on BeanB, and BeanB needs BeanA, a circular dependency is formed. Here are several ways to resolve such issues:

1. Using @Lazy Annotation

Spring allows injecting dependencies lazily. The @Lazy annotation delays the initialization of a bean until it is actually needed. Applying it on one or more beans in the cycle can break the dependency chain.

Example:

@Component
public class BeanA {
    private final BeanB beanB;

    public BeanA(@Lazy BeanB beanB) {
        this.beanB = beanB;
    }
}
@Component
public class BeanB {
    private final BeanA beanA;

    public BeanB(BeanA beanA) {
        this.beanA = beanA;
    }
}

2. Using Setter Injection

Instead of using constructor injection, you can use setter injection for one of the beans involved in the cycle. This allows Spring to create the beans first and then resolve the dependency.

Example:

@Component
public class BeanA {
    private BeanB beanB;

    // Setter injection for the dependency
    public void setBeanB(BeanB beanB) {
        this.beanB = beanB;
    }
}
@Component
public class BeanB {
    private final BeanA beanA;

    // Constructor injection for BeanA
    public BeanB(BeanA beanA) {
        this.beanA = beanA;
    }
}

3. Using @PostConstruct or @Autowired on a Method

You can resolve dependencies after both beans are created by using a method annotated with @PostConstruct or @Autowired.

Example Using @Autowired:

@Component
public class BeanA {
    private BeanB beanB;

    @Autowired
    public void setBeanB(BeanB beanB) {
        this.beanB = beanB;
    }
}
@Component
public class BeanB {
    private final BeanA beanA;

    public BeanB(BeanA beanA) {
        this.beanA = beanA;
    }
}

4. Using @Lookup Annotation

The @Lookup annotation allows Spring to provide a method-level injection. This creates a new instance of the required bean whenever the method is called, avoiding a direct circular dependency.

Example:

@Component
public class BeanA {
    public void someMethod() {
        // BeanB is retrieved via the lookup method
        BeanB beanB = getBeanB();
    }

    @Lookup
    public BeanB getBeanB() {
        return null; // Spring will override this method to inject BeanB
    }
}

5. Refactoring to Avoid the Circular Dependency

A circular dependency often indicates tightly coupled beans or poor architectural design. Refactoring the code to introduce separation of concerns or an intermediary bean can resolve the issue. For example, you can create a third bean that acts as a mediator between BeanA and BeanB.

Example:

@Component
public class BeanMediator {
    @Autowired
    private BeanA beanA;

    @Autowired
    private BeanB beanB;

    public void mediate() {
        // Logic involving both BeanA and BeanB
    }
}

6. Using @Primary or @Qualifier with Proxies

In some cases, using @Primary or @Qualifier with method-level proxies can help in managing circular dependencies, though this should be used sparingly.

7. Using Java Config with @Bean Method

When defining beans in a Java configuration (@Configuration), you can break the cycle by defining one of the beans as a normal object.

Example:

@Configuration
public class AppConfig {

    @Bean
    public BeanA beanA() {
        return new BeanA(beanB());
    }

    @Bean
    public BeanB beanB() {
        return new BeanB();
    }
}

Best Practices

  • Avoid unnecessary circular dependencies in your application by properly designing your classes and their relationships.
  • Refactor your beans to adhere to the Single Responsibility Principle (SRP) and Dependency Inversion Principle (DIP) whenever possible.
  • Use Spring tools, such as @Lazy, strategically rather than as default fixes for design problems.

Building Your First REST API with Spring Boot in Under 10 Minutes

Creating a simple REST API using Spring Boot can be a smooth process, and it is entirely possible to get it done in under 10 minutes. Here’s a step-by-step guide to building your first REST API:

1. Setup Your Spring Boot Project

  1. Go to Spring Initializr.
  2. Configure the project:
    • Project: Maven.
    • Language: Java.
    • Spring Boot Version: Choose the latest stable version.
    • Dependencies: Add Spring Web (this adds support for creating a REST API).
  3. Click to download the project as a .zip file. Generate

  4. Extract the file, then open it in your favorite IDE like IntelliJ IDEA.

2. Create a REST Controller

A REST controller handles HTTP requests and maps them to methods. You will use the @RestController annotation to create a REST API in Spring Boot.
Create a file under src/main/java/com/example/demo/ named : HelloWorldController.java

package com.example.demo;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class HelloWorldController {

    @GetMapping("/hello")
    public String helloWorld() {
        return "Hello, World!";
    }
}
  • Annotations Explained:
    • @RestController: Combines @Controller and @ResponseBody, enabling REST-specific behavior.
    • @RequestMapping: Specifies the base path for this controller (e.g., /api).
    • @GetMapping: Handles HTTP GET requests for the /hello sub-endpoint.

3. Run Your Application

  1. Run your application by navigating to the DemoApplication class and executing the method (Run button in IntelliJ). main
  2. By default, your application will start on port 8080.

4. Test Your REST API

Hello, World!

5. (Optional) Add Another Endpoint Returning JSON

If you want to return a JSON response, create a new endpoint:

@GetMapping("/greet")
public Greeting greetUser() {
    return new Greeting("Welcome to Spring Boot!");
}

Create a new class Greeting.java under the same package:

package com.example.demo;

public class Greeting {

    private String message;

    public Greeting(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
{
    "message": "Welcome to Spring Boot!"
}

6. Complete!

Congratulations, you’ve successfully created your first REST API with Spring Boot!

How do I inject environment-specific values using Spring Profiles?

Spring Profiles provide a flexible way to load environment-specific configurations in a Spring-based application. Here’s a step-by-step guide on how to use them:

Steps to Inject Environment-Specific Values Using Spring Profiles

  1. Define Profile-Specific Properties Files
    Create separate property files for different environments (e.g., development, testing, production). Make sure to name them with a clear convention.

    Examples:

    • application-dev.properties (for development)
    • application-prod.properties (for production)
    • application-test.properties (for testing)

    In these files, define environment-specific values.
    Example (application-dev.properties):

    app.name=MyApp (Dev Environment)
    app.url=http://localhost:8080
    

    Example (application-prod.properties):

    app.name=MyApp (Production)
    app.url=https://myapp.com
    
  2. Activate the Profile
    Use one of the following methods to specify which profile is active:

    • In application.properties or application.yml: Add the spring.profiles.active property.
    spring.profiles.active=dev
    
    • As a Command-Line Argument:
      You can pass the profile during application startup:
    java -jar myapp.jar --spring.profiles.active=prod
    
    • As an Environment Variable:
      Set the SPRING_PROFILES_ACTIVE environment variable:
    export SPRING_PROFILES_ACTIVE=prod
    
  3. Use the @Profile Annotation in Beans (Optional)
    Annotate components or configurations that should only load in specific profiles using the @Profile annotation.

    Example:

    @Component
    @Profile("dev")
    public class DevDatabaseInitializer { 
        public DevDatabaseInitializer() {
           System.out.println("Initializing Development Database...");
        }
    }
    

    The above bean will only be loaded when the “dev” profile is active.

  4. Access Values from Properties Files
    Use @Value or the @ConfigurationProperties annotation to inject values into your code.

    Example:

    @Component
    public class AppConfig {
        @Value("${app.name}")
        private String appName;
    
        @Value("${app.url}")
        private String appUrl;
    
        public void printConfig() {
           System.out.println("App Name: " + appName);
           System.out.println("App URL: " + appUrl);
        }
    }
    
  5. (Optional) Use application.yml for Profile-Specific Configuration
    Instead of multiple property files, you can use a single application.yml with profile-specific sections:

    spring:
      profiles:
        active: dev
    
    ---
    spring:
      profiles: dev
      app:
        name: MyApp (Dev Environment)
        url: http://localhost:8080
    
    ---
    spring:
      profiles: prod
      app:
        name: MyApp (Production)
        url: https://myapp.com
    

Using Spring Profiles allows you to maintain clean and environment-specific configurations, reducing the chance of errors and simplifying the deployment process.

How Dependency Injection Works in Spring Boot Using @Autowired

Dependency Injection (DI) is a fundamental concept in Spring Boot that facilitates the management of dependencies between various components in your application. Spring Boot primarily uses the @Autowired annotation to enable automatic wiring of beans. Below, I’ll explain how Dependency Injection works using @Autowired in Spring Boot, step by step.

What is Dependency Injection?

Instead of creating dependencies manually within components, Spring’s IoC (Inversion of Control) container injects dependencies into an object automatically. This reduces tight coupling and makes code more reusable and flexible.

How Does @Autowired Work?

@Autowired is used to mark a dependency that Spring should handle automatically. It tells Spring, “Find the appropriate bean in the IoC container and inject it here.”

Here’s how it works:

  1. Mark the class as a Spring Bean using @Component, @Service, @Repository, or by defining it explicitly via @Bean in a configuration class.
  2. Use the @Autowired Annotation to inject a required dependency into a field, constructor, or setter method.

Approaches to Using @Autowired

1. Field Injection

This is the simplest form of dependency injection where the field is annotated with @Autowired.

import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;

@Component
public class CarService {

    @Autowired
    private Engine engine;

    public void startCar() {
        engine.start();
    }
}
@Component
public class Engine {
    public void start() {
        System.out.println("Engine started!");
    }
}
  • Pros: Quick and readable.
  • Cons: Field injection is less testable and not recommended for complex applications because the dependency is hard to mock in unit tests.

2. Constructor Injection (Recommended)

Here, dependencies are injected via the constructor. This is the most preferred approach as it promotes immutability and easy testing.

import org.springframework.stereotype.Component;

@Component
public class CarService {

    private final Engine engine;

    @Autowired // Optional in Spring Boot since 4.3+
    public CarService(Engine engine) {
        this.engine = engine;
    }

    public void startCar() {
        engine.start();
    }
}
@Component
public class Engine {
    public void start() {
        System.out.println("Engine started!");
    }
}
  • Pros: Cleaner, better for unit testing, adheres to immutability principles.
  • Cons: Requires explicit constructors, but modern tools (e.g., Lombok) simplify this.

3. Setter Injection

Dependencies are injected via setter methods. This is helpful when a bean property is optional and not required during object creation.

import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;

@Component
public class CarService {

    private Engine engine;

    @Autowired
    public void setEngine(Engine engine) {
        this.engine = engine;
    }

    public void startCar() {
        engine.start();
    }
}
@Component
public class Engine {
    public void start() {
        System.out.println("Engine started!");
    }
}
  • Pros: Useful when you want to inject optional dependencies.
  • Cons: Makes the class mutable and harder to test.

Behind the Scenes

  1. Bean Discovery: The Spring IoC container detects beans annotated with @Component, @Service, @Repository, @Controller, or those defined explicitly in configuration classes.
  2. Dependency Resolution: When @Autowired is detected on a field, constructor, or setter, Spring scans its ApplicationContext to find a matching bean (by type).
  3. Injection: The resolved bean (dependency) is injected into the target where @Autowired is present.

Handling Ambiguity

If multiple beans of the same type are defined, Spring would throw a NoUniqueBeanDefinitionException. You can resolve this by:

  • Using @Qualifier:
@Autowired
@Qualifier("specificEngine")
private Engine engine;
  • Marking a bean as a primary candidate:
@Primary
@Component
public class Engine { ... }

Example: A Working Spring Boot Application

Here’s a complete example of how @Autowired can be used in a Spring Boot application.

Application Entry Point

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootExampleApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootExampleApplication.class, args);
    }
}

Service & Dependency

import org.springframework.stereotype.Service;

@Service
public class GreetingService {

    public String getGreeting() {
        return "Hello, Dependency Injection!";
    }
}
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {

    private final GreetingService greetingService;

    public GreetingController(GreetingService greetingService) {
        this.greetingService = greetingService;
    }

    @GetMapping("/greet")
    public String greet() {
        return greetingService.getGreeting();
    }
}

Run the application, and visiting http://localhost:8080/greet will give you the message:
Hello, Dependency Injection!

Best Practices

  1. Use Constructor Injection for mandatory dependencies.
  2. Avoid Field Injection, except for small applications or testing purposes.
  3. Annotate beans properly using stereotypes like @Service, @Component, etc.
  4. For optional beans, use @Autowired(required = false) or setter injection.
  5. Handle bean ambiguity with @Qualifier or @Primary.