我有一个名为 Screenshot
的实体类和一个声明如下的存储库:
public interface ScreenshotRepository extends JpaRepository<Screenshot, UUID>, ScreenshotRepositoryCustom
自定义存储库的定义如下:
interface ScreenshotRepositoryCustom
和
class ScreenshotRepositoryImpl implements ScreenshotRepositoryCustom {
private final ScreenshotRepository screenshotRepo;
@Autowired
public ScreenshotRepositoryImpl(ScreenshotRepository screenshotRepo) {
this.screenshotRepo = screenshotRepo;
}
这遵循其他 Stack Overflow 问题中的描述:How to add custom method to Spring Data JPA
现在,IntelliJ 向我发出警告:
Autowired members must be defined in a valid Spring bean
我尝试将这些注释添加到 ScreenshotRepositoryImpl
但没有任何效果:
@Repository
@Component
@Service
但没有任何效果。显然有些是错误的,但我正在尝试。正确的注释是什么。
使用@Repository
,我收到此错误:
2017-11-23 12:30:04.064 WARN 20576 --- [ restartedMain] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'screenshotsController' defined in file [C:\Users\pupeno\Documents\Dashman\java\dashmanserver\out\production\classes\tech\dashman\server\controllers\ScreenshotsController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'screenshotRepositoryImpl' defined in file [C:\Users\pupeno\Documents\Dashman\java\dashmanserver\out\production\classes\tech\dashman\server\models\ScreenshotRepositoryImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'screenshotRepositoryImpl': Requested bean is currently in creation: Is there an unresolvable circular reference?
2017-11-23 12:30:04.064 INFO 20576 --- [ restartedMain] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2017-11-23 12:30:04.064 INFO 20576 --- [ restartedMain] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2017-11-23 12:30:04.080 INFO 20576 --- [ restartedMain] utoConfigurationReportLoggingInitializer :
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-11-23 12:30:04.080 ERROR 20576 --- [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
The dependencies of some of the beans in the application context form a cycle:
screenshotsController defined in file [C:\Users\pupeno\Documents\Dashman\java\dashmanserver\out\production\classes\tech\dashman\server\controllers\ScreenshotsController.class]
┌─────┐
| screenshotRepositoryImpl defined in file [C:\Users\pupeno\Documents\Dashman\java\dashmanserver\out\production\classes\tech\dashman\server\models\ScreenshotRepositoryImpl.class]
└─────┘
最佳答案
发生了什么?
您的依赖项形成一个循环:ScreenshotRepository
扩展 ScreenshotRepositoryCustom
,但 ScreenshotRepositoryCustom
实现依赖于 ScreenshotRepository
。由于这个循环,没有一个 Bean 能够完成它们的实例化。
建议的解决方案
在这些场景中,Spring Data 不支持通过构造函数注入(inject)。尝试这样做将导致依赖循环错误。为了能够将 ScreenshotRepository
注入(inject) ScreenShotRepositoryImpl
,您需要通过字段进行注入(inject):
@Repository
public class ScreenshotRepositoryImpl implements ScreenshotRepositoryCustom {
@Autowired
ScreenshotRepository screenshotRepository ;
...
}
关于java - 如何注释自定义 Spring Boot 自定义存储库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47455561/