簡體   English   中英

使用 mongodb 和 redis 作為緩存的 Spring Boot

[英]Spring boot with mongodb and redis as cache

我有一個spring boot項目,我使用mongodb作為我的主數據庫,我想包括redis作為項目的緩存,因為它有很多圖像訪問,並且對於主數據庫來說會很重。

<dependencies>  
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>   
        <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>com.google.firebase</groupId>
        <artifactId>firebase-admin</artifactId>
        <version>8.1.0</version>
    </dependency>
    <!-- Exclude tomcat dependency -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <!-- Include jetty dependency -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jetty</artifactId>
    </dependency>
</dependencies>

那是我當前的依賴項

每當我想添加:

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

我收到此錯誤:

引起:org.springframework.beans.factory.NoUniqueBeanDefinitionException:沒有“org.springframework.data.mapping.context.MappingContext”類型的合格bean可用:預期的單個匹配bean但找到了2:mongoMappingContext,keyValueMappingContext

有誰知道如何解決這個問題?

我最近遇到了完全相同的問題。 您沒有提到您的 Spring Boot 版本是什么,但這讓我在從 2.6.6 升級到 3.0.0-M3 時得到了幫助。 相同的代碼適用於舊版本,所以我開始挖掘......

長話短說,根本原因在於 MongoAuditingRegistrar,所以我猜你正在使用 @EnableMongoAuditing 注釋。 當我禁用審核時,3.0.0-M3 版本也一切正常。

罪魁禍首在這里

definition.addConstructorArgValue(new RuntimeBeanReference(MappingContext.class));

如果您同時使用 Mongo 和 Redis,則有兩個可用的映射上下文,而 Spring 不知道使用哪一個,因為 MappingContext 接口的引用過於通用。

在 2.6.6 版本中,同樣的事情是這樣完成的:

definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_CONSTRUCTOR);

對我有用的快速而骯臟的,可能不是最好和最終的解決方案是創建我自己的@EnableCustomMongoAuditing注釋,而不是:

@Import(MongoAuditingRegistrar.class)

會使用:

@Import(CustomMongoAuditingRegistrar.class)

將上述行更改為:

definition.addConstructorArgValue(new RuntimeBeanReference(MongoMappingContext.class));

我希望它也可以為您工作,即使作為臨時解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM