我有一个名为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]
└─────┘
答案 0 :(得分:2)
您的依赖关系形成一个周期:ScreenshotRepository
扩展ScreenshotRepositoryCustom
,但ScreenshotRepositoryCustom
实施取决于ScreenshotRepository
。由于这个循环,没有一个bean可以完成它们的实例化。
在这些场景中,Spring Data不支持通过构造函数注入。尝试这样做会导致依赖循环错误。为了能够ScreenshotRepository
注入ScreenShotRepositoryImpl
,您需要通过字段进行注射:
@Repository
public class ScreenshotRepositoryImpl implements ScreenshotRepositoryCustom {
@Autowired
ScreenshotRepository screenshotRepository ;
...
}