【问题标题】:NoSuchBeanDefinitionException after update to spring boot 2.2.1更新到 Spring Boot 2.2.1 后的 NoSuchBeanDefinitionException
【发布时间】:2023-03-26 07:41:01
【问题描述】:

将现有代码从 Spring Boot 2.2.0 升级到 2.2.1 后,我遇到了一个奇怪的问题。
似乎我的 spring 数据 jdbc 存储库不再以某种方式被扫描:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'de.thd.dmpk.establishmentmanagement.IEstablishmentRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

此外,启动 2.2.1 中也有此信息调试行:

Spring Data JDBC - Could not safely identify store assignment for repository candidate interface de.thd.dmpk.establishmentmanagement.IEstablishmentRepository. If you want this repository to be a JDBC repository, consider annotating your entities with one of these annotations: org.springframework.data.relational.core.mapping.Table.

当我将所有内容切换回启动 2.2.0 时,信息消息以及上述异常都消失了。

有什么提示吗?

编辑
实体

@Getter
@RequiredArgsConstructor(staticName = "of", access = AccessLevel.PUBLIC, onConstructor = @__({@PersistenceConstructor}))
@EqualsAndHashCode
public final class Establishment {

private final @Id
@With
long establishmentId;

@NotNull
@NotEmpty
@Size(max = 255)
private final
String establishmentName;
}

存储库

interface IEstablishmentRepository extends CrudRepository<Establishment, Long>

到目前为止,如果您不想更改数据库上的表名,则不需要 @Table 注释。此外,@EnableJdbcRepositories 以这种方式扫描每个文档:

如果没有配置基本包,则使用包含 配置类驻留。 https://docs.spring.io/spring-data/jdbc/docs/current/reference/html/#jdbc.java-config

那里发生了奇怪的事情:)

【问题讨论】:

  • 贴出实体和存储库的代码,现在看来你在实体中缺少注释。
  • 您的应用中是否使用了多个数据存储?
  • 我只配置了一个数据源。我使用了一个依赖于spring-boot-starter-data-ldap 的自定义库...
  • 您的存储库中有@Repository 注释吗?
  • 不,因为据我了解,@EnableJdbcRepositories 负责扫描它们。但是尽管如此,将@Repository 放在他们身上并不能完成这项工作 - 仍然得到NoSuchBeanDefinitionException

标签: spring-boot spring-data-jdbc


【解决方案1】:

应该更热情地阅读控制台输出:

Spring Data JDBC - 无法安全地识别存储库候选接口 de.thd.dmpk.establishmentmanagement.IEstablishmentRepository 的存储分配。如果您希望此存储库成为 JDBC 存储库,请考虑使用以下注释之一来注释您的实体:org.springframework.data.relational.core.mapping.Table。

用@Table 注释我的实体就可以了。放入我的实体后一切正常。

这背后的原因是DATAJDBC-437。 当 Spring Data JDBC 与其他 Spring Data Modules 一起使用时,Spring Data JDBC 过去感觉要对所有存储库负责,从而导致每个接口有多个 bean。 为了避免在这种情况下出现这种情况,@Table 注释需要在被视为 JDBC 存储库主题的聚合根上。

【讨论】:

  • 不确定,但我怀疑这是由于DATAJDBC-437,当检测到 Spring Data 的多个模块时会强制执行严格检查(这是由于 Spring Data LDAP 依赖关系所致)。跨度>
  • 我在答案中添加了一些背景。
【解决方案2】:

只是关于这个主题的一些附加信息:
如果您从 spring-boot-starter-parent 包中省略了 spring-data-ldap 依赖项并将其替换为 spring-ldap-core ,那么 spring 数据的多个模块的问题就不再存在了。
因此,如果您只是使用/依赖来自spring-ldap-core 的ldapTemplate,那么一切都很好。

【讨论】:

    【解决方案3】:

    在您的存储库中添加@Repository。

    JPABuddy 默认不这样做,所以我登陆了这里。
    这些结论在 cmets 中已经指出,但为了记录,我们需要一个答案。 :)

    【讨论】:

      猜你喜欢
      • 2018-09-18
      • 2015-01-14
      • 2016-12-06
      • 2015-03-05
      • 2020-03-08
      • 2019-11-14
      • 1970-01-01
      • 1970-01-01
      • 2023-02-10
      相关资源
      最近更新 更多