Spring Boot JPA无法添加自定义存储库

时间:2017-06-29 17:01:05

标签: spring-boot spring-data-jpa

我尝试在Spring启动环境中创建自定义存储库。所以我的所有存储库都可以通过扩展它来实现这种行为。

TestRepository.java

"projects": {
  "YourCoolProject": {
      "root": "",
      ...
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist",
            ...
            "assets": [
              "src/assets", 
              "myOtherStaticFiles"   /* <-- Add *your* directory(s) here */
            ],
            "styles": [
            ...

TestRepositoryImpl.java

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

SampleRepository

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);
}

但是应用程序无法启动并抛出异常。

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

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

更新

的pom.xml

&#13;
&#13;
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.
&#13;
&#13;
&#13;

附加pom.xml。 application.properties没有变化。

1 个答案:

答案 0 :(得分:3)

看起来弹簧启动和弹簧数据的最新里程碑存在问题,如果切换回2.0.0.M1一切正常:

<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数据团队可能会更好。