5

spring-data-jpa을 사용하여 리포지토리의 메서드에 사용자 지정 동작을 구현하려고합니다.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 인스턴스를 주입하려고했지만 아래에 표시된 것과 같은 오류가 있습니다.

이것은 내가 실행하는 간단한 ServerApp 응용 프로그램입니다.

이것은 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 같은 사용자 지정 동작을 구현하기 위해 무엇을 할 수 있습니까?

이 선언에서 @Repository 주석을 제거 :

답변

9

는 클래스에 두 가지 문제가 있습니다

@Repository 
public interface ProductRepositoryCustom { 

이 현재의 문제를 해결하지만 another one이 나타납니다.

이에 대한 해결책은 내가 제거하려고하지 않았다 바보 느낄 수있어, 매우

ProductRepositoryCustomImpl 

ProductRepositoryImpl 
+4

감사의 이름을 변경하는'그 Repository' 주석 @, LOL. 그리고 그 두 번째 문제에 대해서, 나는 그 첫 번째 문제보다 먼저 그 문제에 직면했고,'@EnableJpaRepositories (basePackages = "packages.repo", repositoryImplementationPostfix = 1)를 사용하여 나의'ApplicationContext' config에 대한 몇 가지 커스터마이징을 한 후 약 1 시간의 좌절감으로 해결했습니다. "CustomImpl")' – TheKojuEffect

+0

은 repositoryImplementationPostfix 옵션에 대해 알지 못했습니다. +1 –

+0

당신은 '<저장소 base-package = "packages.repo"repository-impl-postfix = "CustomImpl"/> XML 기반 구성을 사용합니다. 자세한 내용은이 [참조 문서] (http://static.springsource.org/spring-data/data-jpa/docs/current/reference/html/repositories.html#repositories.single-repository-behaviour)를 참조하십시오. – TheKojuEffect