Spring数据JPA自定义通用存储库模式问题

Spring数据JPA自定义通用存储库模式问题,spring,jpa,spring-data,spring-data-jpa,Spring,Jpa,Spring Data,Spring Data Jpa,我在Spring中有以下配置,但由于存储库的Impl类中缺少init方法,自动连接失败。Spring不应该试图通过构造函数初始化bean,但应该使用工厂。。。我错过了一些简单的配置。。。或者我遇到了一只虫子 我正在尝试实现一个单一的通用存储库,其中所有存储库都可以共享特定于映射域类的方法和特定方法 这是我的错误: Error creating bean with name 'auditRepositoryImpl' defined in file AuditRepositoryImpl.clas

我在Spring中有以下配置,但由于存储库的Impl类中缺少init方法,自动连接失败。Spring不应该试图通过构造函数初始化bean,但应该使用工厂。。。我错过了一些简单的配置。。。或者我遇到了一只虫子

我正在尝试实现一个单一的通用存储库,其中所有存储库都可以共享特定于映射域类的方法和特定方法

这是我的错误:

Error creating bean with name 'auditRepositoryImpl' defined in file AuditRepositoryImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.domain.biz.dao.impl.AuditRepositoryImpl]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.domain.biz.dao.impl.AuditRepositoryImpl.<init>()

2014-07-05 08:16:48366调试org.springframework.data.repository.config.RepositoryBeanDefinitionBuilder注册自定义存储库实现:auditRepositoryImpl auditRepositoryImpl
2014-07-05 08:16:48367调试org.springframework.data.repository.config.repositoryconfiguration代理注册存储库:auditRepository-接口:auditRepository-工厂:org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean
//Spring Java配置
@配置
@使能调度
@EnableSpringConfiguration
@导入(EnvConfiguration.class)
@促性腺激素
@EnableJParepositions(repositoryFactoryBeanClass=DefaultRepositoryFactoryBean.class,basePackages={“com.domain.biz.dao”},repositoryImplementationPostfix=“Impl”)
@启用缓存
@EnableTransactionManagement(proxyTargetClass=true)
@ComponentScan(basePackages={“com.domain.biz”})
@订单(2)
公共类AppConfiguration扩展CachingConfigurer支持实现LoadTimeWeavingConfigurer
...
@菜豆
公共接口通用存储库
扩展JPA假设{
...
@菜豆
公共抽象类AbstractRepositoryImpl
扩展SimpleParepository实现GenericRepository{
专用静态记录器日志=LoggerFactory
.getLogger(AbstractRepositoryImpl.class);
私人课堂;
@自动连线
实体管理器实体管理器;
@自动连线
会话工厂会话工厂;
public AbstractRepositoryImpl(类domainClass,EntityManager em){
超级(域名类,em);
}
公共摘要RepositoryImpl(JPA实体信息实体信息,
实体管理器(实体管理器){
超级(实体信息、实体管理器);
}
...
@菜豆
//@Scope(BeanDefinition.Scope\u原型)
公共类GenericRepositoryImpl
扩展AbstractRepositoryImpl实现GenericRepository{
...
公共接口AuditRepositoryCustom{
公共审计(审计审计);
公共接口AuditRepository扩展了GenericRepository、AuditRepositoryCustom{
公共类DefaultRepositoryFactoryBean
扩展JpaRepositoryFactoryBean{
私有静态类RepositoryFactory
扩展JpaRepositoryFactory{
私人实体管理者实体管理者;
公共存储工厂(EntityManager EntityManager){
超级(实体管理器);
this.entityManager=entityManager;
}
@凌驾
受保护类getRepositoryBaseClass(RepositoryMetadata元数据){
//RepositoryMetadata可以安全地忽略,它由
//JPA仓库工厂
//检查超出范围的QueryDslJpaRepository。
返回GenericRepository.class;
}
@凌驾
受保护对象GetTargetPository(RepositoryMetadata元数据){
返回新的GenericRepositoryImpl(
(类)metadata.getDomainType(),this.entityManager);
}
}
@凌驾
受保护的RepositoryFactorySupport createRepositoryFactory(
实体管理器(实体管理器){
返回新的存储工厂(entityManager);
}

对于根本原因,异常非常清楚。您的
AuditRepositoryImpl
既没有无参数构造函数,也没有用
@Inject
/
@Autowired
注释的构造函数。以下是更正的代码,以防有人需要

@NoRepositoryBean
    public interface GenericRepository<T extends Serializable, I extends Serializable>
            extends JpaRepository<T, I> {

        Result truncate();

...

@NoRepositoryBean
public abstract class AbstractRepositoryImpl<T extends Serializable, I extends Serializable>
        extends SimpleJpaRepository<T, I> implements GenericRepository<T, I> {



    public AbstractRepositoryImpl(Class<T> domainClass, EntityManager em) {
        super(domainClass, em);

    }

    public AbstractRepositoryImpl(JpaEntityInformation<T, ?> entityInformation,
            EntityManager entityManager) {
        super(entityInformation, entityManager);

    }

    @Override
    public Result truncate() {


...


public class GenericRepositoryImpl<T extends Serializable, I extends Serializable>
        extends AbstractRepositoryImpl<T, I> implements GenericRepository<T, I> {

    public GenericRepositoryImpl(Class<T> domainClass, EntityManager em) {
        super(domainClass, em);

    }

    public GenericRepositoryImpl(JpaEntityInformation<T, ?> entityInformation,
            EntityManager entityManager) {
        super(entityInformation, entityManager);

    }


...


public interface AuditRepositoryCustom {

    public Audit audit(Audit audit);

....



public interface AuditRepository extends GenericRepository<Audit, Long>,
AuditRepositoryCustom {


...


@NoRepositoryBean
public class AuditRepositoryImpl extends GenericRepositoryImpl<Audit, Long>
        implements AuditRepositoryCustom {

    private static Logger log = LoggerFactory.getLogger(AuditService.class);

    public AuditRepositoryImpl(Class<Audit> domainClass, EntityManager em) {
        super(domainClass, em);

        log.debug("AuditDAO Created...");
        // TODO Auto-generated constructor stub
    }

    @Autowired
    public AuditRepositoryImpl(EntityManager em) {
        super(Audit.class, em);

    }

    public AuditRepositoryImpl(
            JpaEntityInformation<Audit, ?> entityInformation,
            EntityManager entityManager) {
        super(entityInformation, entityManager);

        log.debug("AuditDAO Created...");
        // TODO Auto-generated constructor stub
    }

    @Override
    public Audit audit(Audit audit) {

        return super.save(audit);

    }
@NoRepositoryBean
公共接口通用存储库
扩展JPA假设{
结果截断();
...
@菜豆
公共抽象类AbstractRepositoryImpl
扩展SimpleParepository实现GenericRepository{
public AbstractRepositoryImpl(类domainClass,EntityManager em){
超级(域名类,em);
}
公共摘要RepositoryImpl(JPA实体信息实体信息,
实体管理器(实体管理器){
超级(实体信息、实体管理器);
}
@凌驾
公共结果截断(){
...
公共类GenericRepositoryImpl
扩展AbstractRepositoryImpl实现GenericRepository{
公共GenericRepositoryImpl(类domainClass,EntityManager em){
超级(域名类,em);
}
公共通用存储impl(JPA实体信息实体信息,
实体管理器(实体管理器){
超级(实体信息、实体管理器);
}
...
公共接口AuditRepositoryCustom{
公共审计(审计审计);
....
公共接口AuditRepository扩展了GenericRepository,
审计报告习惯{
...
@菜豆
公共类AuditRepositoryImpl扩展了GenericRepositoryImpl
实现AuditRepositoryCustom{
私有静态记录器log=LoggerFactory.getLogger(AuditService.class);
public AuditRepositoryImpl(类domainClass,EntityManager em){
超级(域名类,em);
调试(“AuditDAO已创建…”);
//TODO自动生成的构造函数存根
}
@自动连线
公共审计报告实施(实体管理器em){
超级(审计类,em);
}
公共审计报告(
JPA实体信息实体信息,
实体管理器(实体管理器){
超级(实体信息,实体
2014-07-05 08:16:48,366 DEBUG  org.springframework.data.repository.config.RepositoryBeanDefinitionBuilder Registering custom repository implementation: auditRepositoryImpl AuditRepositoryImpl 
2014-07-05 08:16:48,367 DEBUG  org.springframework.data.repository.config.RepositoryConfigurationDelegate Registering repository: auditRepository - Interface: AuditRepository - Factory: org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean 







    //Spring Java config
    @Configuration
    @EnableScheduling
    @EnableSpringConfigured
    @Import(EnvConfiguration.class)
    @EnableAspectJAutoProxy
    @EnableJpaRepositories(repositoryFactoryBeanClass = DefaultRepositoryFactoryBean.class, basePackages = { "com.domain.biz.dao" }, repositoryImplementationPostfix = "Impl")
    @EnableCaching
    @EnableTransactionManagement(proxyTargetClass = true)
    @ComponentScan(basePackages = { "com.domain.biz" })
    @Order(2)
    public class AppConfiguration extends CachingConfigurerSupport implements LoadTimeWeavingConfigurer 

    ...

@NoRepositoryBean
public interface GenericRepository<T extends Serializable, I extends Serializable>
        extends JpaRepository<T, I> {

...


@NoRepositoryBean
public abstract class AbstractRepositoryImpl<T extends Serializable, I extends Serializable>
        extends SimpleJpaRepository<T, I> implements GenericRepository<T, I> {

    private static Logger log = LoggerFactory
            .getLogger(AbstractRepositoryImpl.class);

    private Class<T> clazz;

    @Autowired
    EntityManager entityManager;

        @Autowired
        SessionFactory sessionFactory;

        public AbstractRepositoryImpl(Class<T> domainClass, EntityManager em) {
            super(domainClass, em);

        }

        public AbstractRepositoryImpl(JpaEntityInformation<T, ?> entityInformation,
                EntityManager entityManager) {
            super(entityInformation, entityManager);

        }


    ...


        @NoRepositoryBean
        // @Scope( BeanDefinition.SCOPE_PROTOTYPE )
        public class GenericRepositoryImpl<T extends Serializable, I extends Serializable>
                extends AbstractRepositoryImpl<T, I> implements GenericRepository<T, I> {


...

    public interface AuditRepositoryCustom {

        public Audit audit(Audit audit);



    public interface AuditRepository extends GenericRepository<Audit, Long>, AuditRepositoryCustom {




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

    private static class RepositoryFactory<T extends Serializable, I extends Serializable>
            extends JpaRepositoryFactory {

        private EntityManager entityManager;

        public RepositoryFactory(EntityManager entityManager) {
            super(entityManager);

            this.entityManager = entityManager;
        }

        @Override
        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 GenericRepository.class;
        }

        @Override
        protected Object getTargetRepository(RepositoryMetadata metadata) {

            return new GenericRepositoryImpl<T, I>(
                    (Class<T>) metadata.getDomainType(), this.entityManager);
        }
    }

    @Override
    protected RepositoryFactorySupport createRepositoryFactory(
            EntityManager entityManager) {

        return new RepositoryFactory(entityManager);
    }
@NoRepositoryBean
    public interface GenericRepository<T extends Serializable, I extends Serializable>
            extends JpaRepository<T, I> {

        Result truncate();

...

@NoRepositoryBean
public abstract class AbstractRepositoryImpl<T extends Serializable, I extends Serializable>
        extends SimpleJpaRepository<T, I> implements GenericRepository<T, I> {



    public AbstractRepositoryImpl(Class<T> domainClass, EntityManager em) {
        super(domainClass, em);

    }

    public AbstractRepositoryImpl(JpaEntityInformation<T, ?> entityInformation,
            EntityManager entityManager) {
        super(entityInformation, entityManager);

    }

    @Override
    public Result truncate() {


...


public class GenericRepositoryImpl<T extends Serializable, I extends Serializable>
        extends AbstractRepositoryImpl<T, I> implements GenericRepository<T, I> {

    public GenericRepositoryImpl(Class<T> domainClass, EntityManager em) {
        super(domainClass, em);

    }

    public GenericRepositoryImpl(JpaEntityInformation<T, ?> entityInformation,
            EntityManager entityManager) {
        super(entityInformation, entityManager);

    }


...


public interface AuditRepositoryCustom {

    public Audit audit(Audit audit);

....



public interface AuditRepository extends GenericRepository<Audit, Long>,
AuditRepositoryCustom {


...


@NoRepositoryBean
public class AuditRepositoryImpl extends GenericRepositoryImpl<Audit, Long>
        implements AuditRepositoryCustom {

    private static Logger log = LoggerFactory.getLogger(AuditService.class);

    public AuditRepositoryImpl(Class<Audit> domainClass, EntityManager em) {
        super(domainClass, em);

        log.debug("AuditDAO Created...");
        // TODO Auto-generated constructor stub
    }

    @Autowired
    public AuditRepositoryImpl(EntityManager em) {
        super(Audit.class, em);

    }

    public AuditRepositoryImpl(
            JpaEntityInformation<Audit, ?> entityInformation,
            EntityManager entityManager) {
        super(entityInformation, entityManager);

        log.debug("AuditDAO Created...");
        // TODO Auto-generated constructor stub
    }

    @Override
    public Audit audit(Audit audit) {

        return super.save(audit);

    }