我正在尝试使用spring-data-jpa
.
ProductRepository
接口是
@Repository
public interface ProductRepository extends JpaRepository,
ProductRepositoryCustom {
public List findByProductName(String productName);
}
该ProductRepositoryCustom
接口包含一个saveCustom
我想要实现自定义行为的接口。
@Repository
public interface ProductRepositoryCustom {
public Product saveCustom(Product product);
}
这是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);
}
}
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
?