将现有代码从 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
以这种方式扫描每个文档:
If no base package is configured, it uses the package in which the configuration class resides. https://docs.spring.io/spring-data/jdbc/docs/current/reference/html/#jdbc.java-config
那里发生了奇怪的事情:)
最佳答案
应该更热情地阅读控制台输出:
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.
用 @Table
注释我的实体就可以了。放入我的实体后一切正常。
这背后的原因是DATAJDBC-437 .
当 Spring Data JDBC 与其他 Spring Data Modules 一起使用时,Spring Data JDBC 过去感觉负责所有存储库,导致每个接口(interface)有多个 bean。
为了避免在这种情况下 @Table
需要在被视为 JDBC 存储库主题的聚合根上进行注释。
关于spring-boot - 更新到 spring boot 2.2.1 后出现 NoSuchBeanDefinitionException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58912292/