![](/img/trans.png)
[英]How to write a Conditional Custom Query in Spring boot Crud Repository
[英]How to annotate a custom Spring Boot custom repository?
我有一个名为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;
}
这是在另一个堆栈溢出问题中描述的内容: 如何向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不支持通过构造函数进行注入。 尝试这样做将导致依赖周期错误。 为了能够将ScreenshotRepository
注入ScreenShotRepositoryImpl
,您需要通过field进行注入:
@Repository
public class ScreenshotRepositoryImpl implements ScreenshotRepositoryCustom {
@Autowired
ScreenshotRepository screenshotRepository ;
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.