【问题标题】:How to annotate a custom Spring Boot custom repository?如何注释自定义 Spring Boot 自定义存储库?
【发布时间】:2018-05-07 10:09:29
【问题描述】:

我有一个名为 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]
└─────┘

【问题讨论】:

  • @Repository 是正确的注释
  • 为什么投反对票?
  • @Jens @Repository 不起作用,其他问题只是发布到@Repository。我明确表示 @Repository 在我最初的问题中不起作用。
  • doesn't work 不是错误描述,也没有说这不是正确的注释。正如您在错误消息Requested bean is currently in creation: Is there an unresolvable circular reference 中看到的那样,将找到 bean。

标签: java spring javabeans


【解决方案1】:

发生了什么?

你的依赖形成了一个循环:ScreenshotRepository 扩展了ScreenshotRepositoryCustom,但ScreenshotRepositoryCustom 的实现依赖于ScreenshotRepository。由于这个循环,没有一个 bean 可以完成它们的实例化。

建议的解决方案

Spring Data 在这些场景中不支持通过构造函数进行注入。尝试这样做会导致依赖循环错误。为了能够将ScreenshotRepository 注入ScreenShotRepositoryImpl,您需要通过字段进行注入:

@Repository
public class ScreenshotRepositoryImpl implements ScreenshotRepositoryCustom {

    @Autowired
    ScreenshotRepository screenshotRepository ;

    ...

}

【讨论】:

  • 我从未使用过 EntityManager,所以我试图弄清楚它应该如何工作。使用您在此处粘贴的代码,我收到此错误:Common causes of this problem include using a final class or a non-visible class; nested exception is org.springframework.cglib.core.CodeGenerationException: java.lang.reflect.InvocationTargetException--&gt;null
  • 另外,这个答案是否不正确:stackoverflow.com/questions/11880924/… ?
  • 如果你真的需要重用来自ScreenshotRepository的方法,你最好创建一个注入ScreenshotRepositoryScreenshotService以避免循环。
  • 我更新了我的答案。我已经阅读了您链接问题中的 cmets。看来您可以通过现场注入来做到这一点。但是,我不确定它是否适用于 Spring Boot 1.5+,因为一些 cmets 似乎表明它不适用于他们。你可以试一试吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-17
  • 1970-01-01
  • 2016-08-11
  • 2017-12-03
  • 1970-01-01
  • 1970-01-01
  • 2021-12-09
相关资源
最近更新 更多