Creating a Custom Auto-Configuration with Spring Boot

Auto-configuration in Spring Boot simplifies the configuration process by automatically configuring beans based on the dependencies and properties available in the application context. Creating custom auto-configuration enables you to bundle your configurations and make them reusable across different projects. This guide will walk you through creating a custom auto-configuration with Spring Boot 3.2.


  • JDK 17 or later
  • Maven or Gradle
  • IDE (IntelliJ IDEA, Eclipse, etc.)

Step 1: Set Up a New Project

1.1 Create a New Spring Boot Project

Use Spring Initializr to create a new project with the following configuration:

  • Project: Maven Project
  • Language: Java
  • Spring Boot: 3.2.x
  • Dependencies: None (we'll add necessary dependencies manually)

Download and unzip the project, then open it in your IDE.

1.2 Update pom.xml

Update the pom.xml file to include the necessary dependencies and configurations for your custom auto-configuration.

<project xmlns=""


    <description>Custom Spring Boot Auto-Configuration</description>



        <!-- Spring Boot Autoconfigure -->
        <!-- Optional dependencies -->



  • spring-boot-autoconfigure: Provides auto-configuration support.
  • spring-boot-starter-logging: Adds logging support, which is common for most starters.

Step 2: Create Custom Components and Configuration

2.1 Create a Custom Service

Create a class named CustomService in the com.example.customautoconfig.service package.

package com.example.customautoconfig.service;

public class CustomService {

    public String getMessage() {
        return "Hello from CustomService!";


  • CustomService: A simple service class that returns a message.

2.2 Create an Auto-Configuration Class

Create a class named CustomAutoConfiguration in the com.example.customautoconfig.config package.

package com.example.customautoconfig.config;

import com.example.customautoconfig.service.CustomService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

public class CustomAutoConfiguration {

    public CustomService customService() {
        return new CustomService();


  • @Configuration: Indicates that the class can be used by the Spring IoC container as a source of bean definitions.
  • @Bean: Marks a method as a bean producer for the application context.

2.3 Configure Auto-Configuration Metadata

Create a resources/META-INF/spring.factories file to register your auto-configuration class.

# META-INF/spring.factories



  • spring.factories: Registers the CustomAutoConfiguration class to be automatically configured.

Step 3: Test the Custom Auto-Configuration

3.1 Create a Spring Boot Application to Use the Custom Auto-Configuration

Create a new Spring Boot project to test your custom auto-configuration. Use Spring Initializr to create a new project with the following configuration:

  • Project: Maven Project
  • Language: Java
  • Spring Boot: 3.2.x
  • Dependencies: Spring Web

Download and unzip the project, then open it in your IDE.

3.2 Add the Custom Auto-Configuration Dependency

Add the custom auto-configuration dependency to the pom.xml file of your new Spring Boot application.


3.3 Use the Custom Service in Your Application

Create a controller class named HelloController in the com.example.demo.controller package.

package com.example.demo.controller;

import com.example.customautoconfig.service.CustomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

public class HelloController {

    private CustomService customService;

    public String hello() {
        return customService.getMessage();


  • CustomService: The custom service provided by your custom auto-configuration.
  • @Autowired: Injects the CustomService dependency.
  • @GetMapping("/hello"): Maps GET requests to the /hello endpoint and returns a message from the CustomService.

Step 4: Running and Testing the Application

4.1 Run the Application

Run the Spring Boot application using your IDE or the command line:

./mvnw spring-boot:run

4.2 Test the Custom Auto-Configuration

Use a web browser or a tool like Postman to send a GET request to http://localhost:8080/hello. You should see the message "Hello from CustomService!".


In this comprehensive guide, you have learned how to create a custom auto-configuration with Spring Boot. We covered:

  • Setting up a new project for the custom auto-configuration.
  • Creating custom components and an auto-configuration class.
  • Registering the auto-configuration class using spring.factories.
  • Creating a new Spring Boot application to test the custom auto-configuration.
  • Using the custom auto-configuration in the application and verifying its functionality.

By following these steps, you can create custom auto-configurations to encapsulate reusable configurations and simplify the setup of your Spring Boot applications.