7

を使用して、リポジトリのメソッドにカスタム動作を実装しようとしています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ですか?

4

1 に答える 1

11

クラスには 2 つの問題があります。

この宣言から @Repository アノテーションを削除します。

@Repository
public interface ProductRepositoryCustom {

これで現在の問題は解決しますが、の問題が発生します。

これに対する解決策は、名前を変更することです

ProductRepositoryCustomImpl

ProductRepositoryImpl
于 2013-05-05T10:49:26.967 に答える