How to annotate a custom Spring Boot custom repository?

江枫思渺然 提交于 2019-12-12 04:46:05

问题


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

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

Now, IntelliJ is giving me a warning:

Autowired members must be defined in a valid Spring bean

I tried adding these annotations to ScreenshotRepositoryImpl but none of the worked:

  • @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:

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]
└─────┘

回答1:


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.

Proposed solution

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 ;

    ...

}


来源:https://stackoverflow.com/questions/47455561/how-to-annotate-a-custom-spring-boot-custom-repository

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!