Spring Boot
Auto Configuration
Database
Disable
Java Development

Disable all Database related auto configuration in Spring Boot

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

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:

  1. Less Control: With autoconfiguration, Spring Boot makes certain assumptions about your application. In complex scenarios, these assumptions might not always align with your needs.
  2. Performance: Automatically configuring unused components can increase startup times and consume additional resources.
  3. 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.

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:

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}

Method 2: Use application.properties or application.yml

Another way to disable autoconfigurations is by using property files:

In application.properties:

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

In application.yml:

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

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.

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}

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

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

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.


Course illustration
Course illustration

All Rights Reserved.