Spring Boot JPA 无法添加自定义存储库 [英] Spring Boot JPA unable to add custom repository

查看:45
本文介绍了Spring Boot JPA 无法添加自定义存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在 Spring 引导环境中创建自定义存储库.所以我所有的存储库都可以通过扩展它来拥有这种行为.

I tried creating custom repository in Spring boot environment. So all my repositories can have this behavior by extending it.

TestRepository.java

TestRepository.java

@NoRepositoryBean
public interface TestRepository<T> extends JpaRepository<T, Long> {
    Object getMemberValue(Long id, String memberName);
}

TestRepositoryImpl.java

TestRepositoryImpl.java

public class TestRepositoryImpl<T> extends SimpleJpaRepository<T, Long> implements TestRepository<T> {
    public TestRepositoryImpl(JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager) {
        super(entityInformation, entityManager);
    }

    @Override
    public Object getMemberValue(Long id, String memberName) {
        //logic to get member value
        return null;
    }
}

示例存储库

@Repository
public interface UserRepository extends TestRepository<User> {
    List<User> findByNameOrderById(String name);
}

启动配置

@SpringBootApplication
@EnableJpaRepositories(repositoryBaseClass = TestRepositoryImpl.class)
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

但应用程序无法启动并引发以下异常.

But application failed to start and throws below exception.

2017-06-30 00:46:45.378  INFO 5408 --- [  restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2017-06-30 00:46:45.529  WARN 5408 --- [  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testController': Unsatisfied dependency expressed through field 'userRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Cannot create inner bean '(inner bean)#6084d49d' of type [org.springframework.data.repository.core.support.RepositoryFragmentsFactoryBean] while setting bean property 'repositoryFragments'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#6084d49d': Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testRepositoryImplFragment': Cannot resolve reference to bean 'testRepositoryImpl' while setting constructor argument; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testRepositoryImpl' defined in file [D:\Workspace\Y3\EPOD_JAVA\EPOD_Boot\target\classes\com\test\repository\TestRepositoryImpl.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: {}
2017-06-30 00:46:45.529  INFO 5408 --- [  restartedMain] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2017-06-30 00:46:45.530  INFO 5408 --- [  restartedMain] .SchemaDropperImpl$DelayedDropActionImpl : HHH000477: Starting delayed drop of schema as part of SessionFactory shut-down'
2017-06-30 00:46:45.541  INFO 5408 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2017-06-30 00:46:45.547  INFO 5408 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.
2017-06-30 00:46:45.553  INFO 5408 --- [  restartedMain] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2017-06-30 00:46:45.612  INFO 5408 --- [  restartedMain] utoConfigurationReportLoggingInitializer : 

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-06-30 00:46:46.026 ERROR 5408 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in com.test.repository.TestRepositoryImpl required a bean of type 'org.springframework.data.jpa.repository.support.JpaEntityInformation' that could not be found.


Action:

Consider defining a bean of type 'org.springframework.data.jpa.repository.support.JpaEntityInformation' in your configuration.

我是否缺少添加自定义存储库的任何配置?

Am I missing any configuration to add custom repository?

更新

pom.xml

<?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>com.test</groupId>
	<artifactId>test</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>test</name>
	<description>Test project in Spring Boot 2</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.0.M2</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<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.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>

		<dependency>
			<groupId>org.hsqldb</groupId>
			<artifactId>hsqldb</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

	<repositories>
		<repository>
			<id>spring-snapshots</id>
			<name>Spring Snapshots</name>
			<url>https://repo.spring.io/snapshot</url>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
		</repository>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>

	<pluginRepositories>
		<pluginRepository>
			<id>spring-snapshots</id>
			<name>Spring Snapshots</name>
			<url>https://repo.spring.io/snapshot</url>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
		</pluginRepository>
		<pluginRepository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</pluginRepository>
	</pluginRepositories>


</project>

附上pom.xml.application.properties 中没有变化.

pom.xml attached. No changes in application.properties.

推荐答案

看起来 spring boot 和 spring data 的最新里程碑有问题,如果你切换回 2.0.0.M1一切正常:

Looks like there's an issue with the latest milestone of spring boot and spring data, if you switch back to 2.0.0.M1 everything works ok:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.M1</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

可能最好将问题报告给 spring 数据团队.

Probably it's better to report the issue to spring data team.

这篇关于Spring Boot JPA 无法添加自定义存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆