Auto-Configuration

Configuration Based on the Classpath

Auto-configuration means Spring Boot looks at which libraries are on the classpath and which beans you already defined, then creates sensible beans for everything else.

  • spring-webmvc + Tomcat on the classpath → configure DispatcherServlet and start Tomcat on port 8080.
  • Jackson on the classpath → configure an ObjectMapper for JSON.
  • A JDBC driver + spring.datasource.url → create a connection pool (DataSource).
  • You define the bean yourself → Boot backs off and uses yours.

The Idea in Plain Java

Example

Auto-configuration uses conditions such as "class is present" and "bean is missing". Here we check real classes with Class.forName (the MySQL driver is not on this classpath):

import java.util.LinkedHashMap;
import java.util.Map;

public class Main {
    static boolean onClasspath(String className) {
        try { Class.forName(className); return true; }
        catch (ClassNotFoundException e) { return false; }
    }

    public static void main(String[] args) {
        Map<String, Object> userBeans = new LinkedHashMap<>();
        userBeans.put("objectMapper", "MyCustomObjectMapper");     // user defined their own

        Map<String, String> autoConfigs = new LinkedHashMap<>();
        autoConfigs.put("httpClient", "java.net.http.HttpClient");
        autoConfigs.put("objectMapper", "java.lang.String");       // pretend: Jackson present
        autoConfigs.put("dataSource", "com.mysql.cj.jdbc.Driver");

        autoConfigs.forEach((bean, requiredClass) -> {
            if (!onClasspath(requiredClass)) {
                System.out.println("SKIP   " + bean + ": " + requiredClass + " not on classpath");
            } else if (userBeans.containsKey(bean)) {
                System.out.println("BACKOFF " + bean + ": user bean " + userBeans.get(bean) + " wins");
            } else {
                System.out.println("CREATE " + bean + " (default)");
            }
        });
    }
}

Output

CREATE httpClient (default)
BACKOFF objectMapper: user bean MyCustomObjectMapper wins
SKIP   dataSource: com.mysql.cj.jdbc.Driver not on classpath

See the Report

Example

Start the app with --debug to print the condition evaluation report: which auto-configurations matched and why.

./mvnw spring-boot:run -Dspring-boot.run.arguments=--debug

Example output

============================
CONDITIONS EVALUATION REPORT
============================
Positive matches:
-----------------
   DispatcherServletAutoConfiguration matched:
      - @ConditionalOnClass found required class 'org.springframework.web.servlet.DispatcherServlet'
   JacksonAutoConfiguration matched:
      - @ConditionalOnClass found required class 'com.fasterxml.jackson.databind.ObjectMapper'

Negative matches:
-----------------
   DataSourceAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required class 'org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType'

Note: How auto-configuration is built internally (@Conditional, writing your own starter) is covered in the Spring Boot advance level.