我試圖在Spring引導環境中創建自定義存儲庫。所以我所有的存儲庫都可以通過擴展它來實現這種行爲。Spring Boot JPA無法添加自定義存儲庫
TestRepository.java
@NoRepositoryBean
public interface TestRepository<T> extends JpaRepository<T, Long> {
Object getMemberValue(Long id, String memberName);
}
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;
}
}
SampleRepository
@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);
}
}
但是應用程序無法啓動並拋出異常。
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.
我缺少任何配置添加自定義庫?
UPDATE
的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"
\t \t xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
\t <modelVersion>4.0.0</modelVersion>
\t <groupId>com.test</groupId>
\t <artifactId>test</artifactId>
\t <version>0.0.1-SNAPSHOT</version>
\t <packaging>jar</packaging>
\t <name>test</name>
\t <description>Test project in Spring Boot 2</description>
\t <parent>
\t \t <groupId>org.springframework.boot</groupId>
\t \t <artifactId>spring-boot-starter-parent</artifactId>
\t \t <version>2.0.0.M2</version>
\t \t <relativePath/> <!-- lookup parent from repository -->
\t </parent>
\t <properties>
\t \t <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
\t \t <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
\t \t <java.version>1.8</java.version>
\t </properties>
\t <dependencies>
\t \t <dependency>
\t \t \t <groupId>org.springframework.boot</groupId>
\t \t \t <artifactId>spring-boot-starter-data-jpa</artifactId>
\t \t </dependency>
\t \t <dependency>
\t \t \t <groupId>org.springframework.boot</groupId>
\t \t \t <artifactId>spring-boot-starter-web</artifactId>
\t \t </dependency>
\t \t <dependency>
\t \t \t <groupId>org.apache.commons</groupId>
\t \t \t <artifactId>commons-lang3</artifactId>
\t \t </dependency>
\t \t <dependency>
\t \t \t <groupId>org.projectlombok</groupId>
\t \t \t <artifactId>lombok</artifactId>
\t \t \t <optional>true</optional>
\t \t </dependency>
\t \t <dependency>
\t \t \t <groupId>org.hsqldb</groupId>
\t \t \t <artifactId>hsqldb</artifactId>
\t \t </dependency>
\t \t <dependency>
\t \t \t <groupId>org.springframework.boot</groupId>
\t \t \t <artifactId>spring-boot-starter-test</artifactId>
\t \t \t <scope>test</scope>
\t \t </dependency>
\t </dependencies>
\t <build>
\t \t <plugins>
\t \t \t <plugin>
\t \t \t \t <groupId>org.springframework.boot</groupId>
\t \t \t \t <artifactId>spring-boot-maven-plugin</artifactId>
\t \t \t </plugin>
\t \t </plugins>
\t </build>
\t <repositories>
\t \t <repository>
\t \t \t <id>spring-snapshots</id>
\t \t \t <name>Spring Snapshots</name>
\t \t \t <url>https://repo.spring.io/snapshot</url>
\t \t \t <snapshots>
\t \t \t \t <enabled>true</enabled>
\t \t \t </snapshots>
\t \t </repository>
\t \t <repository>
\t \t \t <id>spring-milestones</id>
\t \t \t <name>Spring Milestones</name>
\t \t \t <url>https://repo.spring.io/milestone</url>
\t \t \t <snapshots>
\t \t \t \t <enabled>false</enabled>
\t \t \t </snapshots>
\t \t </repository>
\t </repositories>
\t <pluginRepositories>
\t \t <pluginRepository>
\t \t \t <id>spring-snapshots</id>
\t \t \t <name>Spring Snapshots</name>
\t \t \t <url>https://repo.spring.io/snapshot</url>
\t \t \t <snapshots>
\t \t \t \t <enabled>true</enabled>
\t \t \t </snapshots>
\t \t </pluginRepository>
\t \t <pluginRepository>
\t \t \t <id>spring-milestones</id>
\t \t \t <name>Spring Milestones</name>
\t \t \t <url>https://repo.spring.io/milestone</url>
\t \t \t <snapshots>
\t \t \t \t <enabled>false</enabled>
\t \t \t </snapshots>
\t \t </pluginRepository>
\t </pluginRepositories>
</project>
pom.xml的附接。 application.properties中沒有更改。
你的代碼工作正常,你能顯示你的依賴和配置嗎? – ledniov
@ledniov:謝謝你的回覆。問題用pom.xml更新 – Maheshkumar
現在,將'@ NoRepositoryBean'添加到'TestRepositoryImpl'。 – mp911de