在spring-data-jpa中为存储库方法实现自定义行为

The*_*ect 7 spring dependency-injection circular-dependency spring-data-jpa

我正在尝试使用存储库中的方法实现自定义行为spring-data-jpa.

ProductRepository接口是


@Repository
public interface ProductRepository extends JpaRepository,
        ProductRepositoryCustom {

    public List findByProductName(String productName);

}
Run Code Online (Sandbox Code Playgroud)

ProductRepositoryCustom接口包含saveCustom我想要实现自定义行为的接口.


@Repository
public interface ProductRepositoryCustom {
    public Product saveCustom(Product product);
}
Run Code Online (Sandbox Code Playgroud)

这是ProductRepositoryCustom接口的实现.saveCustom这里的方法只是一个例子.我真正想要做的是定义一个自定义方法,使其包含一系列涉及核心JpaRepository方法的指令.为此,我尝试注入ProductRepository实例,但我得到错误,如下所示.


public class ProductRepositoryCustomImpl implements ProductRepositoryCustom {
    @Inject
    private ProductRepository repo;

    @Override
    public Product saveCustom(Product product) {
                // other executions of methods in ProductRepository(repo)
        return repo.save(product);
    }

}
Run Code Online (Sandbox Code Playgroud)

这是ServerApp我运行的简单应用程序.


public class ServerApp {

    public static void main(String[] args) {
                ApplicationContext context = new AnnotationConfigApplicationContext(
                AppContext.class);
        ProductRepository repo = context.getBean(ProductRepository.class);
        Product testProduct = new Product();
        testProduct.setProductName("Test Product");
        repo.saveCustom(testProduct);
    }
}
Run Code Online (Sandbox Code Playgroud)

当我启动时,这是程序的堆栈跟踪ServerApp.


Exception in thread "main" org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'productRepositoryCustomImpl': Bean with name 'productRepositoryCustomImpl' has been injected into other beans [productRepository] in its raw version as part of a circular reference, but has eventually been wrapped. This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using 'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:551)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    at org.springframework.context.annotation.AnnotationConfigApplicationContext.(AnnotationConfigApplicationContext.java:73)
    at devopsdistilled.operp.server.ServerApp.main(ServerApp.java:16)
Run Code Online (Sandbox Code Playgroud)

我该怎么做才能实现自定义行为saveCustom

Fra*_*man 11

您的课程有两个问题:

从此声明中删除@Repository注释:

@Repository
public interface ProductRepositoryCustom {
Run Code Online (Sandbox Code Playgroud)

这将解决您当前的问题,但会出现另一个问题.

解决方法是重命名

ProductRepositoryCustomImpl
Run Code Online (Sandbox Code Playgroud)

ProductRepositoryImpl
Run Code Online (Sandbox Code Playgroud)

  • 非常感谢,我感觉很愚蠢,我没有尝试删除`@ Repository`注释,LOL.关于第二个问题,我在第一个问题之前遇到了这个问题,经过大约一个小时的挫折后,通过使用`@EnableJpaRepositories(basePackages ="packages.repo",repositoryImplementationPostfix =对我的`ApplicationContext`配置进行一些自定义)解决了这个问题. "CustomImpl")` (4认同)