Overriding and Debugging Auto-configuration
A teammate adds the H2 database to library-service for quick experiments and forgets about it. Weeks later, the app starts on a server without its PostgreSQL settings and, instead of failing loudly, quietly runs on an empty in-memory database. Auto-configuration did exactly what it was told. When Boot's choices surprise you, you need two skills: seeing what it decided, and changing it.
The rule
Ask the report why; then override with a property, a bean, or an exclude.
Seeing what Boot decided. Start the app with --debug (or set debug=true) and Boot prints a condition evaluation report:
============================
CONDITIONS EVALUATION REPORT
============================
Positive matches:
DataSourceAutoConfiguration matched:
- @ConditionalOnClass found required classes 'javax.sql.DataSource' ...
Negative matches:
MongoDataAutoConfiguration:
Did not match:
- @ConditionalOnClass did not find required class 'com.mongodb.client.MongoClient'Positive matches were applied; negative matches were skipped, each with the reason. With Actuator, the same report is available at /actuator/conditions, and /actuator/beans lists every bean in the context.
Changing what Boot decided, from gentlest to strongest:
- A property. Most auto-configured beans read properties:
server.port,spring.datasource.url,spring.jpa.open-in-view=false. Try this first. - Your own bean. Define a bean of the same type and
@ConditionalOnMissingBeanmakes Boot's version back off. - An exclude. Switch off a whole auto-configuration class:
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class LibraryApplication { ... }or, without touching code, spring.autoconfigure.exclude= followed by the class's full name.