gpt4 book ai didi

java - 如何在 Spring-Data-JPA 的自定义实现存储库中使用我的基本存储库方法?

转载 作者:行者123 更新时间:2023-12-03 18:42:17 25 4
gpt4 key购买 nike

我遵循了创建和自定义存储库的引用指南,并提出了以下内容:

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

这些当然是不可访问的,因为 ChildRepositoryImpl实现 ChildCustomRepository而不是 ChildRepository ,否则我将不得不编写基本 CRUD 和自定义接口(interface)的实现。

所以我尝试注入(inject) ChildRepository对 Impl 来说:
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 creating bean with name 'childDao': FactoryBean threw exception on object creation; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:149)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:109)
at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1442)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:248)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:848)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:790)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:438)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:416)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:549)
at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:150)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:303)
... 65 more
Caused by: java.lang.NullPointerException
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:125)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:41)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142)
... 78 more

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

最佳答案

我知道的一种解决方案是实现接口(interface) ApplicationContextAwareChildRepositoryImpl上课并像 applicationContext.getBean(<bean name>) 一样阅读 bean .
我不想实现这一点,但这是我目前知道的唯一选择。

关于java - 如何在 Spring-Data-JPA 的自定义实现存储库中使用我的基本存储库方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11500796/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com