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 Perform Preorder Non-Recursive Traversal of a Given Binary Tree
So sánh HashMap và HashSet trong Java
Java Program to Implement Quick Sort Using Randomization
Java Program to Implement the Checksum Method for Small String Messages and Detect
Java Program to Generate Random Numbers Using Middle Square Method
Lớp Collectors trong Java 8
Exception Handling in Java
Lấy ngày giờ hiện tại trong Java
Java Program to Implement PrinterStateReasons API
Java Optional as Return Type
Filtering and Transforming Collections in Guava
Unsatisfied Dependency in Spring
Assert an Exception is Thrown in JUnit 4 and 5
Removing all Nulls from a List in Java
Java Program to Implement Hash Tables Chaining with Doubly Linked Lists
Java Program to Implement LinkedBlockingDeque API
Java Program to Implement the Vigenere Cypher
Hướng dẫn Java Design Pattern – Memento
Java Program to Implement Graham Scan Algorithm to Find the Convex Hull
Loại bỏ các phần tử trùng trong một ArrayList như thế nào trong Java 8?
Guide to the Volatile Keyword in Java
Spring Boot - Servlet Filter
Build a REST API with Spring and Java Config
New Features in Java 12
Java – Reader to InputStream
Mệnh đề if-else trong java
How to Read a Large File Efficiently with Java
Java Program to Find Minimum Number of Edges to Cut to make the Graph Disconnected
Lớp Arrarys trong Java (Arrays Utility Class)
Getting a File’s Mime Type in Java
The Dining Philosophers Problem in Java
Base64 encoding và decoding trong Java 8