我正在使用 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/