如何注释自定义Spring Boot自定义存储库? [英] How to annotate a custom Spring Boot custom repository?
问题描述
我有一个名为Screenshot
的实体类和一个声明为这样的存储库:
I have an entity class called Screenshot
and and a repository declared as this:
public interface ScreenshotRepository extends JpaRepository<Screenshot, UUID>, ScreenshotRepositoryCustom
自定义存储库的定义如下:
The custom repository is defined like this:
interface ScreenshotRepositoryCustom
和
class ScreenshotRepositoryImpl implements ScreenshotRepositoryCustom {
private final ScreenshotRepository screenshotRepo;
@Autowired
public ScreenshotRepositoryImpl(ScreenshotRepository screenshotRepo) {
this.screenshotRepo = screenshotRepo;
}
这是在另一个堆栈溢出问题中描述的内容:如何向Spring Data JPA添加自定义方法
This is following what's described in this other Stack Overflow question: How to add custom method to Spring Data JPA
现在,IntelliJ给我一个警告:
Now, IntelliJ is giving me a warning:
Autowired members must be defined in a valid Spring bean
我尝试将这些注释添加到ScreenshotRepositoryImpl
中,但没有起作用:
I tried adding these annotations to ScreenshotRepositoryImpl
but none of the worked:
-
@Repository
-
@Component
-
@Service
@Repository
@Component
@Service
,但无济于事.显然有些是错误的,但我正在尝试.什么是正确的注释.
but none work. Obviously some are wrong but I was experimenting. What's the correct annotation.
使用@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都无法完成其实例化.
What is happening?
Your dependencies form a cycle: ScreenshotRepository
extends ScreenshotRepositoryCustom
, but the ScreenshotRepositoryCustom
implementation depends on ScreenshotRepository
. Because of this cycle, none of the beans can complete their instantiation.
在这些情况下,Spring Data不支持通过构造函数进行注入.尝试这样做将导致依赖周期错误.为了能够将ScreenshotRepository
注入到ScreenShotRepositoryImpl
中,您需要通过字段进行注入:
Spring Data does not support injection via constructor in these scenarios. Attempting to do so will result in a dependency cycle error. To be able to inject ScreenshotRepository
into ScreenShotRepositoryImpl
, you'd need to do injection via field:
@Repository
public class ScreenshotRepositoryImpl implements ScreenshotRepositoryCustom {
@Autowired
ScreenshotRepository screenshotRepository ;
...
}
这篇关于如何注释自定义Spring Boot自定义存储库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!