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

标签 spring dependency-injection circular-dependency spring-data-jpa

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

ProductRepository 接口(interface)是


@Repository
public interface ProductRepository extends JpaRepository,
        ProductRepositoryCustom {

    public List findByProductName(String productName);

}

ProductRepositoryCustom 接口(interface)包含一个 saveCustom,我想在其中实现自定义行为。


@Repository
public interface ProductRepositoryCustom {
    public Product saveCustom(Product product);
}

这是ProductRepositoryCustom接口(interface)的实现。 这里的方法saveCustom只是一个例子。我真正想做的是定义一个自定义方法,使其包含一系列涉及核心 JpaRepository 方法的指令。为此,我尝试注入(inject) 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);
    }

}

这是我运行的简单的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);
    }
}

这是我启动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)

如何实现 saveCustom 等自定义行为?

最佳答案

您的类(class)有两个问题:

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

@Repository
public interface ProductRepositoryCustom {

这将解决您当前的问题,但是 another one会出现。

解决这个问题的方法是重命名

ProductRepositoryCustomImpl

ProductRepositoryImpl

关于spring - 在 spring-data-jpa 中对存储库方法实现自定义行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16370502/

相关文章:

javascript - RequireJS、循环依赖和导出 "Magic"方法

java - 内容协商 : How to serve other than the highest ranking type from accept header

java - Hibernate 尝试获取用户时出现 Spring 安全错误

unit-testing - 摆脱子组件对象的 'new' 运算符

c# - 在 ASP .NET Core Web API Controller 中注入(inject) Serilog 的 ILogger 接口(interface)

c++ - 循环依赖类中的 Typedef

c++ - boost::shared_ptr 循环中断与weak_ptr

java - Spring MVC 基于 Java 的配置 - 未检测到 servlet 调度程序

java - Spring 中是否有 ServiceLoader 的类似物以及如何使用它?

AngularJS 将服务注入(inject)多个 Controller