Disable Spring Data Auto Configuration – 禁用Spring数据自动配置

最后修改: 2019年 7月 8日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.介绍

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

在这个快速教程中,我们将探讨在Spring Boot中禁用数据库自动配置的两种不同方法。这在测试时会很方便

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

我们将说明Redis、MongoDB和Spring Data JPA的例子。

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

我们先看一下基于注释的方法,然后再看一下属性文件的方法。

2. Disable Using Annotation

2.禁止使用注释

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

让我们从MongoDB例子开始。我们将看一下需要排除的类。

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

Similarly, we’ll look at disabling auto-configuration for Redis:

同样地,我们将看看如何禁用Redis的自动配置。

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

Finally, we’ll look at disabling auto-configuration for Spring Data JPA:

最后,我们将看看如何为Spring Data JPA禁用自动配置。

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

3. Disable Using Property File

3.禁止使用属性文件

We can also disable auto-configuration using the property file.

我们还可以使用属性文件禁用自动配置。

We’ll first explore it with MongoDB:

我们首先用MongoDB来探索。

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

Now we’ll disable it for Redis:

现在我们要为Redis停用它。

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

Finally, we’ll disable it for Spring Data JPA:

最后,我们将为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

4.测试

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

为了测试,我们将检查自动配置类的Spring Bean在我们的应用上下文中是否缺席。

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

我们将从MongoDB的测试开始。我们将验证MongoTemplateBean是否缺席。

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

Now we’ll check for JPA. For JPA, the DataSource bean will be absent:

现在我们将检查JPA。对于JPA,DataSource bean将不存在。

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

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

最后,对于Redis,我们将检查我们的应用程序上下文中的RedisTemplate bean。

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

5. Conclusion

5.结论

In this brief article, we learned how to disable Spring Boot auto-configuration for different databases.

在这篇简短的文章中,我们学习了如何禁用Spring Boot对不同数据库的自动配置。

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

本文中所有例子的源代码都可以在GitHub上找到。