spring-boot - 扩展 Spring Data 的 SimpleJpaRepository 时无法自动装配 JpaEntityInformation
问题描述
描述
JpaEntityInformation 是扩展 SimpleJpaRepository 时的强制依赖项。不幸的是 Spring 没有找到这个 bean(尽管它为 EntityManager 找到了),我不明白为什么。
请注意我在“My BaseRepository implementation”代码中的评论,因为它可能与问题有关。
错误
悬停依赖项时来自 Intellij IDEA
Could not autowire. No beans of 'JpaEntityInformation<T, ?>' type found.
启动应用程序时从控制台
ConfigServletWebServerApplicationContext :
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'baseRepositoryImpl' defined in file [E:\project\seed-perso\target\classes\fr\seed\perso\repository\BaseRepositoryImpl.class]:
Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type 'org.springframework.data.jpa.repository.support.JpaEntityInformation<?, ?>' available:
expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
配置
我做了最简单的配置。
--
Maven配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>seed-perso</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.3</version> <!-- Current last version -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
</dependency>
</dependencies>
</project>
运行配置
@SpringBootApplication
@EnableJpaRepositories(repositoryBaseClass = BaseRepositoryImpl.class)
public class APIApplication {
public static void main(String[] args) {
SpringApplication.run(APIApplication.class, args);
}
}
我的基础存储库
package fr.seed.perso.repository;
@NoRepositoryBean
public interface BaseRepository<T, ID> extends JpaRepository<T, ID> {
}
我的 BaseRepository 实现
package fr.seed.perso.repository;
@Repository // Should not be required because of the configuration "repositoryBaseClass = BaseRepositoryImpl.class", but without it the app start without instiating BaseRepositoryImpl, as if it was not considered a bean.
public class BaseRepositoryImpl<T, ID> extends SimpleJpaRepository<T, ID> implements BaseRepository<T, ID> {
private final EntityManager entityManager;
public BaseRepositoryImpl(JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager) {
super(entityInformation, entityManager);
this.entityManager = entityManager;
}
}
相关堆栈溢出问题
- 没有“org.springframework.data.jpa.repository.support.JpaEntityInformation”类型的限定bean
- Spring Boot JPA 无法添加自定义存储库-> 使用已接受答案中的 spring 版本不起作用。
像这样工作的例子
- https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.customize-base-repository
- https://www.baeldung.com/spring-data-jpa-method-in-all-repositories
- https://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-adding-custom-methods-into-all-repositories/
解决方案
推荐阅读
- reactjs - Highchart 的极坐标图:在相交区域系列上应用渐变颜色
- python - 在geojson边界外遮罩叶图
- html - 如何解决这个关于 div 元素的验证错误?
- java - 线程“主”java.util.regex.PatternSyntaxException 中的异常:在索引 0 附近悬空元字符 '*' *
- c - 如何从 int 指针数组中检索值?(上下文:在 pthread_create() 中传递数组)
- c# - 在 asp.net 网络表单上更新或显示进度标签
- c# - 尝试通过检查用户是否存在于列表中来登录用户。C#
- awk - 如何使用 awk 找到文本中连续最多的两个单词?
- node.js - 电子浏览器视图:当我调整嵌入浏览器视图的浏览器窗口大小时显示像素值
- linux - Linux Shell 脚本 - 挂载和备份文件