Spring Boot
Auto Configuration
Database
Disable
Java Development

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:

  1. Less control: Spring Boot makes assumptions about your application that may not fit complex setups.
  2. Performance: configuring unused components can slow startup and use extra resources.
  3. 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:

java
1import org.springframework.boot.SpringApplication;
2import org.springframework.boot.autoconfigure.SpringBootApplication;
3import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
4import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
5
6@SpringBootApplication(exclude = {
7    DataSourceAutoConfiguration.class,
8    HibernateJpaAutoConfiguration.class
9})
10public class Application {
11    public static void main(String[] args) {
12        SpringApplication.run(Application.class, args);
13    }
14}

Exclude with spring.autoconfigure.exclude

The same exclusion in a property file, which is convenient for profile-specific or test configuration.

application.properties:

properties
spring.autoconfigure.exclude=\
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration

application.yml:

yaml
1spring:
2  autoconfigure:
3    exclude: 
4      - org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
5      - org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration

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:

java
1import org.springframework.context.annotation.Bean;
2import org.springframework.context.annotation.Configuration;
3import org.springframework.jdbc.datasource.DriverManagerDataSource;
4
5@Configuration
6public class CustomDatabaseConfig {
7
8    @Bean
9    public DriverManagerDataSource dataSource() {
10        if(customCondition()) {
11            return new DriverManagerDataSource();
12        } else {
13            return null;
14        }
15    }
16
17    private boolean customCondition() {
18        // Determine whether to create the bean
19        return true;
20    }
21}

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.
FeatureDefault BehaviorImpact of DisablingManual Steps Needed
Autoconfiguration of DataSourceAutomatically configures a default DataSourceNo default DataSource will be configuredManually define a DataSource bean
HibernateJpaAutoConfigurationAutomatically sets up JPA and Hibernate entitiesHibernate session management becomes manualConfigure EntityManagerFactory and transactions manually
Additional Starters (e.g., JPA)Automatically configures based on jars in classpathNo auto configuration for ORM layersCustom ORM and datasource setup required

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design