如何在 Spring-Data-JPA 的自定义实现存储库中使用我的基本存储库方法? [英] How to use my base repository methods in custom implementation repository in Spring-Data-JPA?

查看:27
本文介绍了如何在 Spring-Data-JPA 的自定义实现存储库中使用我的基本存储库方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循了创建和自定义存储库的参考指南,并提出了以下建议:

I followed the reference guide for creating and customizing Repositories and came up with the following:

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

    @Override
    protected RepositoryFactorySupport createRepositoryFactory(
        EntityManager entityManager) {

        return new MyBasicRepositoryFactory<T, I>(entityManager);
    }

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

        private EntityManager entityManager;

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

            this.entityManager = entityManager;
        }

        @Override
        protected Object getTargetRepository(RepositoryMetadata metadata) {

            return new AlisDaoImpl<T, I>((Class<T>) metadata.getDomainType(),
                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 MyBasicRepository.class;
        }
}





@NoRepositoryBean
public interface MyBasicRepository<T extends MyEntity, KEY extends Serializable> extends CrudRepository<T, KEY> {

    void customBaseFoo(T entity);

}

public interface ChildCustomRepository{

    void customChildFoo();
}

public interface ChildRepository extends MyBasicRepository<Child, Integer>, ChildCustomRepository {

    void findByName(String name);
}

现在关于customChildFoo() 的实现,我想调用ChildRepository.findByName 或者甚至MyBasicRepository.customBaseFoo().

now on the implementation of customChildFoo(), I would like to call ChildRepository.findByName or perhaps even MyBasicRepository.customBaseFoo().

这些当然是不可访问的,因为 ChildRepositoryImpl 实现了 ChildCustomRepository 而不是 ChildRepository,否则我将不得不编写基本的实现CRUD 和自定义界面.

These of course are not accessible since the ChildRepositoryImpl implements ChildCustomRepository and not ChildRepository, otherwise I would have to wrote the implementation for the basic CRUD and custom interface as well.

所以我尝试将 ChildRepository 注入到 Impl 中:

And so I tried to inject ChildRepository to the Impl as such:

public class ChildRepositoryImpl implements ChildCustomRepository{
    @Resource
    private ChildRepository base;

    @Override
    public void customChildFoo(){
        Child child = base.findByName();
        // do some logic with found child here
        base.customBaseFoo(child);            
    }

但它失败的原因是:Caused by: org.springframework.beans.factory.BeanCreationException: Error create bean with name 'childDao': FactoryBean 在对象创建时抛出异常;嵌套异常是 java.lang.NullPointerException在 org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:149)在 org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:109)在 org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1442)在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:248)在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)在 org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:848)在 org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:790)在 org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)在 org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:438)在 org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:416)在 org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:549)在 org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:150)在 org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)在 org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:303)... 65 更多引起:java.lang.NullPointerException在 org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:125)在 org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:41)在 org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142)... 78个

那么,如何才能做到这一点?

So, how can accomplish that?

推荐答案

我知道的一个解决方案是在 ChildRepositoryImpl 类中实现接口 ApplicationContextAware 并像 applicationContext.getBean().我不想实施这个,但这是我目前知道的唯一选择.

One solution I know is to implement interface ApplicationContextAware in ChildRepositoryImpl class and read the bean like applicationContext.getBean(<bean name>). I don't want to implement this but this is the only option I know as of now.

这篇关于如何在 Spring-Data-JPA 的自定义实现存储库中使用我的基本存储库方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆