Skip to content
Commitwise
Menu

What does Spring Boot's auto-configuration do, and how do you change what it set up?

BackendMid-level

In shortBoot ships conditional configuration classes that create beans when a library is on the classpath and you haven't defined your own. Change it with properties and customizers; defining your own bean replaces the auto-configured one entirely.

Model answer

@SpringBootApplication includes @EnableAutoConfiguration, which loads the auto-configuration classes listed by every jar on the classpath. Each one is guarded by conditions:

  • @ConditionalOnClass: only if a library is present (Jackson, a JDBC driver, Redis).
  • @ConditionalOnMissingBean: only if you haven’t defined that bean yourself.
  • @ConditionalOnProperty: only if a setting is on.

So adding spring-boot-starter-web gives you Tomcat, Spring MVC and a configured JSON mapper without any code.

Changing it, from lightest to heaviest:

  1. Properties in application.yml (server.port, spring.jackson.*).
  2. Customizers, beans that adjust what Boot builds (Jackson2ObjectMapperBuilderCustomizer, RestClientCustomizer).
  3. Your own bean of the same type. The auto-configured one backs off completely, and its properties stop applying, which is a common surprise.
  4. Excluding an auto-configuration (@SpringBootApplication(exclude = …)), rarely needed.

To see what happened and why, run with --debug for the conditions evaluation report, or call /actuator/conditions.

What they're looking for

Conditions (@ConditionalOnClass, @ConditionalOnMissingBean, @ConditionalOnProperty), the back-off rule, how to debug it (--debug, /actuator/conditions), and preferring properties or customizers over replacing beans.

Common mistakes

  • Describing it as "magic" without the conditions behind it.
  • Not knowing that your own bean switches off the auto-configured one, including its properties.
  • Excluding whole auto-configurations to change one setting.

More backend questions

Esc

Type to search the whole site.