首页 > 解决方案 > 扩展 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;
    }
}

相关堆栈溢出问题

像这样工作的例子

标签: spring-bootspring-data-jpaspring-datarepositoryautowired

解决方案


推荐阅读