Disable database auto-configuration in Spring Boot
Codemia Knowledge Hub ·
To disable Spring Boot's database auto-configuration, exclude DataSourceAutoConfiguration and HibernateJpaAutoConfiguration, either in the exclude attribute of @SpringBootApplication or with the spring.autoconfigure.exclude property. After that, Spring Boot will not create a DataSource or JPA setup for you.
Why turn database auto-configuration off
Auto-configuration sets up components based on the jars on the classpath, environment variables and other settings. That saves boilerplate, but has trade-offs:
- Less control: Spring Boot makes assumptions about your application that may not fit complex setups.
- Performance: configuring unused components can slow startup and use extra resources.
- Debugging: problems caused by implicit configuration can be harder to track down.
Typical reasons to disable the database part:
- Custom database setup that the defaults do not support.
- Faster startup by skipping database components you do not need.
- Security requirements where explicit configuration is preferred.
- Tests where you want to mock or simulate different environments.
Exclude in @SpringBootApplication
The simplest approach is the exclude attribute:
Exclude with spring.autoconfigure.exclude
The same exclusion in a property file, which is convenient for profile-specific or test configuration.
application.properties:
application.yml:
Defining your own database configuration
You can also write your own configuration classes and control what gets created, for example with @ConditionalOnMissingBean or @ConditionalOnProperty. The example below does not use those annotations; it defines its own DataSource bean and decides in code whether to create it:
What you must configure yourself afterwards
- Manual configuration: everything that was automatic now has to be set up by hand. Do not overlook crucial settings.
- Dependencies: make sure custom setups have all the dependencies they need to avoid runtime failures.
- Testing: check that your manual configuration matches what the auto-configured version provided.
| 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 |
Related reading
- Disable ONLY_FULL_GROUP_BY
- Disable redis when many timeouts using spring boot
- Disconnect nodes in Cassandra
- Discuss the main issues governing concurrency control in a large distributed database environment
- Disable Basic Authentication while using Spring Security Java configuration
- Disable Cloudformation in Spring Cloud AWS
- Disk Space not freed up even after deleting keyspace from cassandra db and compaction
- Distributed C++ game server which use database
System design interview problems on this topic
- Design a Distributed OLTP Database: solution and walkthrough
- Design a Wide Column Database: solution and walkthrough
- Design a Key Value Store: solution and walkthrough
- Design a Web Cache: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.