Disable all Database related auto configuration in Spring Boot
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Spring Boot autoconfiguration is a powerful feature that takes much of the boilerplate work out of configuring applications, particularly when it comes to database interactions. While convenient, there are scenarios where you may want to disable certain autoconfigurations—especially with databases—either for optimization or for more customized control. This article will guide you through how to disable all database-related autoconfiguration in Spring Boot and why you might consider doing so.
Understanding Spring Boot Auto Configuration
Spring Boot's autoconfiguration feature automatically configures components based on the present jar dependencies, environment variables, and other available settings. This eliminates the need for setting them up manually in most cases, but it comes with some trade-offs:
- Less Control: With autoconfiguration, Spring Boot makes certain assumptions about your application. In complex scenarios, these assumptions might not always align with your needs.
- Performance: Automatically configuring unused components can increase startup times and consume additional resources.
- Debugging: Problems can arise due to implicit configurations, and tracking these issues may become more challenging.
When to Disable Database Autoconfiguration
There are several scenarios where you might want to disable database-related autoconfiguration:
- Custom Database Setup: When you need to configure the database using custom settings that are not supported out-of-the-box.
- Performance Optimization: To reduce startup time by disabling unnecessary database components.
- Security Requirements: For environments with stringent security requirements where explicit configuration is preferable.
- Testing Specific Configurations: During testing, you might want to mock or simulate different environments.
How to Disable Database-Related Auto Configuration
Method 1: Use exclude in @SpringBootApplication
The simplest approach to exclude specific autoconfigurations is by using the exclude attribute in the @SpringBootApplication annotation. For example, if you want to disable DataSourceAutoConfiguration and HibernateJpaAutoConfiguration:
Method 2: Use application.properties or application.yml
Another way to disable autoconfigurations is by using property files:
In application.properties:
In application.yml:
Method 3: Custom Spring Boot Configuration
You can also create your own configuration classes to selectively exclude autoconfiguration by using @ConditionalOnMissingBean or @ConditionalOnProperty. This way, you can have full control over what gets auto-configured.
Potential Pitfalls and Considerations
- Manual Configuration: When you disable autoconfiguration, you must manually configure everything that was previously automatic. Ensure that you don't overlook any crucial settings.
- Ensure Dependencies: When using custom setups, make sure all necessary dependencies are resolved to prevent runtime issues.
- Testing and Debugging: Verify that your manual configurations meet the same criteria and coverage as their autoconfigured counterparts.
Summary Table
| Feature | Default Behavior | Impact of Disabling | Manual Steps Needed |
Autoconfiguration
of DataSource | Automatically configures a default DataSource | No default DataSource will be configured | Manually define a DataSource bean |
HibernateJpaAutoConfiguration | Automatically sets up JPA and Hibernate entities | Hibernate session management becomes manual | Configure EntityManagerFactory and transactions manually |
| Additional Starters (e.g., JPA) | Automatically configures based on jars in classpath | No auto configuration for ORM layers | Custom ORM and datasource setup required |
Conclusion
Disabling database-related autoconfiguration in Spring Boot can offer more control, improved performance, and enhanced debugging capabilities. However, it also introduces complexity that comes with manual configuration. By balancing between these choices, you can tailor your Spring Boot applications to better suit specific business and technical needs.
Related reading
- Disable ONLY_FULL_GROUP_BY
- Disable ONLY_FULL_GROUP_BY
- Disable redis when many timeouts using spring boot
- Disconnect nodes in Cassandra
- Disable Basic Authentication while using Spring Security Java configuration
- Disable Cloudformation in Spring Cloud AWS
- Discuss the main issues governing concurrency control in a large distributed database environment
- Disk Space not freed up even after deleting keyspace from cassandra db and compaction

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.