【问题标题】:Disable Redis AutoConfig in spring boot when testing测试时在spring boot中禁用Redis AutoConfig
【发布时间】:2018-02-08 18:34:56
【问题描述】:

我在使用 Spring Boot 进行测试时尝试禁用 Redis。我已禁用我的配置,但自动配置创建了默认连接并失败,因为它无法连接到不存在的服务。对于测试,我满足于只使用基本的内存缓存或无操作缓存。那也行不通。这是我尝试过的:

根据this issue,我将上述配置添加到我的测试应用程序属性中

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration

但是。这让我更进一步。但最终我得到了 NoSuchBeanDefinitionException redisTemplate - 这是因为 redisReferenceResolver 正在尝试查找。

现在看看我的调试器,它试图水合的 bean 是: org.springframework.data.redis.core.convert.ReferenceResolverImpl 来自 spring-data-redis:1.8.0.RELEASE 来自此依赖项:compile('org.springframework.boot:spring-boot-starter-data-redis') 。我承认,bean 名称有点误导。它实际解析的类型不是

唯一对 redis 的引用是在我们的休眠支持中。

有人能解释一下如何关闭它进行测试吗?

【问题讨论】:

    标签: java spring spring-boot redis spring-data-redis


    【解决方案1】:
    • 尝试在您的测试属性文件中排除这两个自动配置类:
      spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration

    • 排除 org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
      并设置:spring.data.redis.repositories.enabled=false

    【讨论】:

    • 当我返回导致我这个问题的代码时,我会试一试。谢谢
    【解决方案2】:

    使用 YAML 语法(和 Spring Boot):

    spring.autoconfigure:
      exclude:
        - org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
        - org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration
    

    如果您的应用上下文中有SystemEnvironmentPropertySource,您可以使用环境变量SPRING_AUTOCONFIGURE_EXCLUDE 用逗号分隔项目:

    SPRING_AUTOCONFIGURE_EXCLUDE=org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration
    

    【讨论】:

      【解决方案3】:

      如果您不想更改任何文件/代码,也可以使用环境变量来执行此操作:

      SPRING_AUTOCONFIGURE_EXCLUDE=org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration
      

      【讨论】:

        猜你喜欢
        • 2020-02-15
        • 2023-01-23
        • 2019-09-06
        • 2017-06-09
        • 1970-01-01
        • 2016-01-02
        • 1970-01-01
        • 2019-03-30
        • 2018-10-31
        相关资源
        最近更新 更多