【发布时间】:2017-04-28 15:18:22
【问题描述】:
我正在使用带有 Spring Data JPA 的 Spring Boot,只有一个 @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 以某种方式被创建了。我该如何解决这个问题?
【问题讨论】:
-
您的 SORepository 是否扩展了 Spring Data Repository(或类型层次结构中的某些内容)?
-
不,这就是我在这里提供它的原因。 SORepository 只是一个简单的接口。
-
你在某个地方定义了 bean SORepositoryImpl 吗?也许像这样的java配置 @Bean public SORepositoryImpl sORepositoryImpl() { return new SORepositoryImpl(); }
-
你的@Configuration 类呢?可能您正在那里创建一个实例(ID 为“soRepositoryImpl”的实例)。
-
我认为@Slavus 可能是对的:我发现重现此问题的唯一方法是显式定义一个名称为(bean 创建方法)
soRepositoryImpl的 bean,但也通过 a 引用此 bean不匹配的名称(例如@Bean public String foo(SORepository soRepository))没有匹配项,也没有最好的spring猜测。
标签: spring spring-boot spring-data spring-data-jpa