liveBook · Manning

Spring Boot autoconfiguration

As explained in Spring in Action, Sixth Edition

A Spring Boot mechanism that conditionally configures application components based on the classpath, properties, environment, and other application conditions.

Spring Boot autoconfiguration sets up application components when the application starts. It makes reasonable choices based on libraries on the classpath, environment variables, and other conditions, so you can get useful behavior without writing the equivalent Java or XML configuration yourself. section 1.1

It builds on two familiar Spring ideas: component scanning discovers classes to register as beans, and autowiring supplies the beans they depend on. Autoconfiguration goes further by configuring components needed to make available libraries work together. You can still add explicit configuration when the automatic choices do not meet your needs. section 1.1 section 1.5

Listing 1.2 The Taco Cloud bootstrap class
package tacos;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication                                        #1
public class TacoCloudApplication {
 
  public static void main(String[] args) {
    SpringApplication.run(TacoCloudApplication.class, args);  #2
  }
 
}

The bootstrap class shows the small amount of code needed to start the application. Its @SpringBootApplication annotation includes @EnableAutoConfiguration; SpringApplication.run() creates the application context. section 1.2.2

Adding starter dependencies makes the effect visible:

  • With Web and Thymeleaf starters, startup configuration enables Spring MVC, embedded Tomcat, and a Thymeleaf view resolver. section 1.3.6
  • With the Spring Security starter, startup configuration supplies basic security. Requests require authentication, a login page appears, and a generated password is written to the log. section 5.1

The practical approach is to add the dependencies your application needs, run it, and see what Boot provides before writing configuration. Autoconfiguration removes setup work; it does not replace the code that implements your application’s features. section 1.3.6 section 1.4.2

How Spring in Action, Sixth Edition covers it

How deep it goes
Explains triggering inputs and visible results, but does not trace autoconfiguration’s internal implementation.
Examples
Shows Web and Thymeleaf starters configuring a web application, then a security starter producing a login page.
What it assumes
Introduces beans, component scanning, autowiring, and the bootstrap annotation while working from a Java application build.
What you can do after
Apply it via Spring Boot starters to obtain working defaults before adding explicit configuration.
How it connects
Connects autoconfiguration to component scanning, autowiring, starter dependencies, and the Spring application context.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.