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:

1
2
3
4
@SpringBootApplication(exclude = {
    MongoAutoConfiguration.class,
    MongoDataAutoConfiguration.class
})

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

1
2
3
4
@SpringBootApplication(exclude = {
    RedisAutoConfiguration.class,
    RedisRepositoryAutoConfiguration.class
})

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

1
2
3
4
5
@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:

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

Now, let’s disable it for Redis:

1
2
3
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:

1
2
3
4
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:

1
2
3
4
@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:

1
2
3
4
@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:

1
2
3
4
@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:

Multi Dimensional ArrayList in Java
Hướng dẫn sử dụng biểu thức chính quy (Regular Expression) trong Java
Java Program to Represent Graph Using Linked List
Java Program to Implement AA Tree
Guide to Spring @Autowired
Java Program to Sort an Array of 10 Elements Using Heap Sort Algorithm
Uploading MultipartFile with Spring RestTemplate
Java Program to find the maximum subarray sum using Binary Search approach
Spring Cloud Series – The Gateway Pattern
Java Program to Perform Addition Operation Using Bitwise Operators
Generating Random Numbers in a Range in Java
Performance Difference Between save() and saveAll() in Spring Data
Java Program to find the maximum subarray sum O(n^2) time(naive method)
Java – Write an InputStream to a File
Hướng dẫn Java Design Pattern – Service Locator
Request Method Not Supported (405) in Spring
Java Program to Implement Interval Tree
Converting Between a List and a Set in Java
Java Program to Implement the MD5 Algorithm
Posting with HttpClient
So sánh Array và ArrayList trong Java
HttpClient 4 – Follow Redirects for POST
Java Program to Implement Hash Tables
Java Program to Find the Median of two Sorted Arrays using Binary Search Approach
Java Program to Implement Ternary Heap
How to Kill a Java Thread
Java Program to find the number of occurrences of a given number using Binary Search approach
Java Program to Implement Sorted Vector
Java Program to find the peak element of an array using Binary Search approach
How to Delay Code Execution in Java
Java Program to Implement Aho-Corasick Algorithm for String Matching
Java Program to Implement TreeMap API