Spring JPA 2.0存储库/工厂不工作

Spring JPA 2.0存储库/工厂不工作,spring,jpa,spring-data-jpa,repository,Spring,Jpa,Spring Data Jpa,Repository,我正在尝试实现一个定制的JavaSpringJPA存储库,如中所述。 似乎我的spring配置坚持以标准方式创建存储库,而不是使用给定的MyRepositoryFactoryBean,给我 Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.myapp.repository.impl.DocumentRepositoryImpl]: No de

我正在尝试实现一个定制的JavaSpringJPA存储库,如中所述。 似乎我的spring配置坚持以标准方式创建存储库,而不是使用给定的MyRepositoryFactoryBean,给我

Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.myapp.repository.impl.DocumentRepositoryImpl]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.myapp.repository.impl.DocumentRepositoryImpl.<init>()
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:83)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1006)
... 43 more
Caused by: java.lang.NoSuchMethodException: com.myapp.repository.impl.DocumentRepositoryImpl.<init>()
at java.lang.Class.getConstructor0(Class.java:2730)
at java.lang.Class.getDeclaredConstructor(Class.java:2004)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:78)

我在网上查看了各种资源,但我无法区分代码的不同。欢迎任何提示

您需要
com.myapp.repository.impl.DocumentRepositoryImpl

public DocumentRepositoryImpl(){}

Spring首先通过调用默认构造函数(无参数)实例化您在应用程序上下文中声明的bean,然后使用setter注入其他bean

谢谢你的快速回答。但是父类SimpleParepository有2个构造函数,每个构造函数都必须提供2个参数。[断言失败]-此参数是必需的;它不能为null org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getMetadata(JpaEntityInformationSupport.java:58),位于org.springframework.data.jpa.repository.support.SimpleJpaRepository.(SimpleJpaRepository.java:91),位于com.myapp.repository.impl.MyRepositoryImpl.(MyRepositoryImpl.java:18)在com.myapp.repository.impl.DocumentRepositoryImpl.And中,此处的PersonRepoImpl类()也没有默认构造函数。
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/data/jpa"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:beans="http://www.springframework.org/schema/beans"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd">

<repositories base-package="com.myapp.repository"
     factory-class="com.myapp.repository.impl.MyRepositoryFactoryBean"/>
<!-- entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="transactionManager"  -->
package com.myapp.repository.impl;

@NoRepositoryBean
public interface MyRepository<T, ID extends Serializable> extends JpaRepository<T, ID> {
package com.myapp.repository.impl;

@NoRepositoryBean
public class MyRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements MyRepository<T, ID> {

private EntityManager entityManager;

public MyRepositoryImpl(Class<T> domainClass, EntityManager entityManager) {
    super(domainClass, entityManager);

    // This is the recommended method for accessing inherited class dependencies.
    this.entityManager = entityManager;
}

public void sharedCustomMethod(ID id) {
    // implementation goes here
}   
}
package com.myapp.repository.impl;

import java.io.Serializable;

import javax.persistence.EntityManager;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.support.JpaRepositoryFactory;
import org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.core.support.RepositoryFactorySupport;

public class MyRepositoryFactoryBean<R extends JpaRepository<T, I>, T, I extends Serializable> extends JpaRepositoryFactoryBean<R, T, I> {

protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) {
    return new MyRepositoryFactory(entityManager);
}

private static class MyRepositoryFactory<T, I extends Serializable> extends JpaRepositoryFactory {
    private EntityManager entityManager;

    public MyRepositoryFactory(EntityManager entityManager) {
        super(entityManager);
        this.entityManager = entityManager;
    }

    protected Object getTargetRepository(RepositoryMetadata metadata) {
        return new MyRepositoryImpl<T, I>((Class<T>) metadata.getDomainType(), entityManager);
    }

    protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
        // The RepositoryMetadata can be safely ignored, it is used by the JpaRepositoryFactory
        // to check for QueryDslJpaRepository's which is out of scope.
        return MyRepositoryImpl.class;
    }
}
}
package com.myapp.repository;

public interface DocumentRepository { // extends MyRepository<Document, Long>

public Document findByDocumentHash(String hashCode);

public Document findById(long id);

}
package com.myapp.repository.impl;

import java.io.Serializable;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

public class DocumentRepositoryImpl<Document, ID extends Serializable> extends MyRepositoryImpl<Document, Serializable> {

    private static final long serialVersionUID = 1L;

        public DocumentRepositoryImpl(Class<Document> domainClass, EntityManager entityManager) {
        super(domainClass, entityManager);
    }
package com.myapp.web.controller;

@Controller
@RequestMapping(value = "/documents")
public class DocumentController {

@Autowired
private DocumentRepository documentRepository;

@RequestMapping(method = RequestMethod.GET)
public ModelAndView list(HttpServletRequest request) {
    this.documentRepository. ...
}
public DocumentRepositoryImpl(){}