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

标签 spring jpa spring-data spring-data-jpa

我在 Spring 中有以下配置,但由于存储库的 Impl 类中缺少 init 方法, Autowiring 失败。 Spring 不应该尝试通过构造函数来初始化 bean,但它应该使用 Factory……我错过了一些简单的配置……或者我遇到了错误。

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

这是我的错误:

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>()

另一方面,我的 CustomFactory 似乎没有被取走。

2014-07-05 08:16:48,343 DEBUG  org.springframework.data.repository.config.RepositoryComponentProvider Identified candidate component class: file [InventoryRepository.class] 

...

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

最佳答案

异常的根本原因非常清楚。您的 AuditRepositoryImpl 没有无参数构造函数或使用 @Inject/@Autowired 注释的构造函数。

关于Spring Data JPA 自定义通用存储库模式问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24586953/

相关文章:

mongodb - Spring Data MongoDB 在 org.springframework.data.mapping.PropertyPath 找不到类型的属性

java - 如何在添加 mvc 时解决 Spring MVC 中的 XmlBeanDefinitionStoreException :annotation-driven?

java - 具有 DTO 和 EO 的 DRY 原则,用于启用 spring 和 hibernate 的 webapp

spring - 列出所有部署的休息端点(spring-boot,tomcat)

java - 将 pdf/excel 文件动态传输到 Flex UI 应用程序

java - Spring Boot Hibernate @OneToMany 集合总是空的

java - 使用抽象类(不带表)持久保存层次结构的最佳方法是什么

java - Junit 测试用例因抽象方法而失败

jpa - 抽象模式类型未知

spring - 如何使用 spring 存储库进行 bean 验证?