Spring Boot 检测到 2 个相同的存储库 bean

标签 spring spring-boot spring-data spring-data-jpa

我正在使用 Spring Boot 和 Spring Data JPA,只有一个 @SpringBootApplication。我还有一个存储库类,例如:

package com.so;
public interface SORepository {
    //methods
}

并实现

@Repository("qualifier")
@Transactional(readOnly = true)
public class SORepositoryImpl implements SORepository {
   //methods
}

问题是,当我启动应用程序时,出现以下错误:

Parameter 0 of constructor in com.so.SomeComponent required a single bean, but 2 were found:
    - qualifier: defined in file [path\to\SORepositoryImpl.class]
    - soRepositoryImpl: defined in file [path\to\SORepositoryImpl.class]

因此,如您所见,以某种方式创建了一个存储库类的 2 个 bean。我该如何解决这个问题?

最佳答案

您可以使用创建了 Proxy 元素的 Spring Data JPA 方法,然后将其注入(inject)公共(public)类 SORepositoryImpl:

public interface Proxy() extends JpaRepository<Element, Long>{
     Element saveElement (Element element); //ore other methods if you want}

比:

@Repository
@Transactional(readOnly = true)
public class SORepositoryImpl implements SORepository {

@Autowired
private Proxy proxy;

   //end realisation of methods from interface SORepository 
}

关于Spring Boot 检测到 2 个相同的存储库 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41123195/

相关文章:

java - Spring 和 H2 数据库 - 无法连接到 h2 数据库

spring-data - 在 Spring 中使用 Greenplum

mysql - jpa/spring/hibernate中的简单乐观锁问题

java - Jasypt 停止维护了吗?使用 Spring Boot 进行加密时使用什么

java - 何时以及如何创建 cglib-proxied 组件实例

java - 更新多对多关系以包含新字段

sorting - 按 Spring Data 中的 boolean 值排序

java - SpringBoot JUnit 和 @Value

java - Spring 如何向 session 变量添加新的 _csrf token ?

mysql - JHipster - 运行 liquibase :diff fails due to "Access denied"