简体   繁体   English

如何注释自定义Spring Boot自定义存储库?

[英]How to annotate a custom Spring Boot custom repository?

I have an entity class called Screenshot and and a repository declared as this: 我有一个名为Screenshot的实体类和一个声明为以下内容的存储库:

public interface ScreenshotRepository extends JpaRepository<Screenshot, UUID>, ScreenshotRepositoryCustom

The custom repository is defined like this: 定制存储库的定义如下:

interface ScreenshotRepositoryCustom

and

class ScreenshotRepositoryImpl implements ScreenshotRepositoryCustom {
    private final ScreenshotRepository screenshotRepo;

    @Autowired
    public ScreenshotRepositoryImpl(ScreenshotRepository screenshotRepo) {
        this.screenshotRepo = screenshotRepo;
    }

This is following what's described in this other Stack Overflow question: How to add custom method to Spring Data JPA 这是在另一个堆栈溢出问题中描述的内容: 如何向Spring Data JPA添加自定义方法

Now, IntelliJ is giving me a warning: 现在,IntelliJ给我警告:

Autowired members must be defined in a valid Spring bean

I tried adding these annotations to ScreenshotRepositoryImpl but none of the worked: 我尝试将这些注释添加到ScreenshotRepositoryImpl但没有一个起作用:

  • @Repository
  • @Component
  • @Service

but none work. 但无济于事。 Obviously some are wrong but I was experimenting. 显然有些错误,但我正在尝试。 What's the correct annotation. 什么是正确的注释。

With @Repository , I get this error: 使用@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]
└─────┘

What is happening? 怎么了?

Your dependencies form a cycle: ScreenshotRepository extends ScreenshotRepositoryCustom , but the ScreenshotRepositoryCustom implementation depends on ScreenshotRepository . 您的依存关系形成一个循环: ScreenshotRepository扩展了ScreenshotRepositoryCustom ,但是ScreenshotRepositoryCustom实现依赖于ScreenshotRepository Because of this cycle, none of the beans can complete their instantiation. 由于这个周期,所有Bean都无法完成其实例化。

Proposed solution 拟议的解决方案

Spring Data does not support injection via constructor in these scenarios. 在这些情况下,Spring Data不支持通过构造函数进行注入。 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: 为了能够将ScreenshotRepository注入ScreenShotRepositoryImpl ,您需要通过field进行注入:

@Repository
public class ScreenshotRepositoryImpl implements ScreenshotRepositoryCustom {

    @Autowired
    ScreenshotRepository screenshotRepository ;

    ...

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM