Disable Spring Data Auto Configuration

1. Introduction

In this quick tutorial, we’ll explore two different ways to disable database auto-configuration in Spring Boot, which can come in handy, say, when testing.

We’ll see examples for Redis, MongoDB, and Spring Data JPA.

Firstly, we’ll start by looking at the annotation-based approach and then we’ll look at the property file approach.

2. Disable Using Annotation

Let’s start with the MongoDB example, we’ll look at classes that need to be excluded:

@SpringBootApplication(exclude = {
    MongoAutoConfiguration.class, 
    MongoDataAutoConfiguration.class
})

Similarly, let’s look at disabling auto-configuration for Redis:

@SpringBootApplication(exclude = {
    RedisAutoConfiguration.class, 
    RedisRepositoryAutoConfiguration.class
})

Finally, let’s look at disabling auto-configuration for Spring Data JPA:

@SpringBootApplication(exclude = {
    DataSourceAutoConfiguration.class, 
    DataSourceTransactionManagerAutoConfiguration.class, 
    HibernateJpaAutoConfiguration.class
})

3. Disable Using Property File

We can also disable auto-configuration using the property file, let’s first explore it with MongoDB:

spring.autoconfigure.exclude= \
  org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration, \
  org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration

Now, let’s disable it for Redis:

spring.autoconfigure.exclude= \
  org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration, \
  org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration

Similarly, let’s disable it for Spring Data JPA:

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

4. Testing

For testing, we’ll check that the Spring beans for the auto-configured classes are absent in our application context.

Let’s start with the test for MongoDB. We’ll verify if the MongoTemplate bean is absent:

@Test(expected = NoSuchBeanDefinitionException.class)
public void givenAutoConfigDisabled_whenStarting_thenNoAutoconfiguredBeansInContext() { 
    context.getBean(MongoTemplate.class); 
}

Now, let’s check for JPA. For JPA, the DataSource bean will be absent:

@Test(expected = NoSuchBeanDefinitionException.class)
public void givenAutoConfigDisabled_whenStarting_thenNoAutoconfiguredBeansInContext() {
    context.getBean(DataSource.class);
}

Similarly, for Redis we’ll check the RedisTemplate bean in our application context:

@Test(expected = NoSuchBeanDefinitionException.class)
public void givenAutoConfigDisabled_whenStarting_thenNoAutoconfiguredBeansInContext() {
    context.getBean(RedisTemplate.class);
}

5. Conclusion

In conclusion, in this quick tutorial, we’ve explored the ways to disable Spring Boot auto-configuration for different databases.

The source code for all examples in the article is available on GitHub.

Related posts:

Java Program to Implement Fibonacci Heap
Giới thiệu Json Web Token (JWT)
File Upload with Spring MVC
DynamoDB in a Spring Boot Application Using Spring Data
Convert Hex to ASCII in Java
Spring REST API + OAuth2 + Angular (using the Spring Security OAuth legacy stack)
Spring @Primary Annotation
Spring WebFlux Filters
Java Program to Implement the Bin Packing Algorithm
Java Program to Implement the Schonhage-Strassen Algorithm for Multiplication of Two Numbers
Batch Processing with Spring Cloud Data Flow
Java Program to Implement Doubly Linked List
OAuth2 for a Spring REST API – Handle the Refresh Token in Angular
Java Program to do a Breadth First Search/Traversal on a graph non-recursively
Guide to the Fork/Join Framework in Java
Spring Boot - Unit Test Cases
Difference Between Wait and Sleep in Java
Collect a Java Stream to an Immutable Collection
Java Program to Implement Suffix Array
Giới thiệu thư viện Apache Commons Chain
REST Web service: HTTP Status Code và xử lý ngoại lệ RESTful web service với Jersey 2.x
Java Program to Find the Longest Path in a DAG
JPA/Hibernate Persistence Context
HttpClient 4 – Follow Redirects for POST
TreeSet và sử dụng Comparable, Comparator trong java
Spring Boot - Thymeleaf
Java Program to Check if a Matrix is Invertible
Exploring the New Spring Cloud Gateway
Getting Started with Custom Deserialization in Jackson
Calling Stored Procedures from Spring Data JPA Repositories
Cachable Static Assets with Spring MVC
Java Program to Implement Self Balancing Binary Search Tree