5

Ich versuche, benutzerdefiniertes Verhalten zu einer Methode in Repository mit spring-data-jpa zu implementieren.Implementieren benutzerdefiniertes Verhalten zu einer Repository-Methode in Spring-Data-Jpa

Die ProductRepository Schnittstellen ist


@Repository 
public interface ProductRepository extends JpaRepository, 
     ProductRepositoryCustom { 

    public List findByProductName(String productName); 

} 

Die ProductRepositoryCustom Schnittstelle enthalten eine saveCustom, auf die ich benutzerdefiniertes Verhalten implementieren möchten.


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

Dies ist die Implementierung für ProductRepositoryCustom Schnittstelle. Die Methode saveCustom hier ist nur ein Beispiel. Was ich wirklich tun möchte, ist eine benutzerdefinierte Methode so zu definieren, dass sie eine Reihe von Anweisungen enthält, die die Kernmethoden JpaRepository betreffen. Dafür habe ich versucht, ProductRepository Instanzen zu injizieren, aber ich habe Fehler wie unten gezeigt.


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); 
    } 

} 

Das ist einfach ServerApp Anwendung, die ich laufen.


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); 
    } 
} 

Dies ist die Stacktrace-Programm, wenn ich die ServerApp starten.


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) 

Was kann ich tun benutzerdefinierte Verhalten wie saveCustom zu implementieren?

Antwort

9

Es gibt zwei Probleme mit Ihren Klassen:

entfernen @Repository Anmerkung aus dieser Erklärung:

@Repository 
public interface ProductRepositoryCustom { 

Dies wird Ihr aktuelles Problem lösen, aber another one erscheint.

Die Lösung hierfür ist

ProductRepositoryCustomImpl 

zu

ProductRepositoryImpl 
+4

Vielen Dank umbenennen, ich fühle mich dumm ich nicht zu entfernen versuchte, dass '@ Repository' Anmerkung, LOL. Und über dieses zweite Problem habe ich dieses Problem vor dem ersten Problem gesehen und es nach ungefähr einer Stunde der Frustration gelöst, indem ich einige Anpassungen an meiner 'ApplicationContext'-Konfiguration vorgenommen habe, indem ich' @EnableJpaRepositories (basePackages = "packages.repo", repositoryImplementationPostfix = verwendet habe) "CustomImpl") ' – TheKojuEffect

+0

wusste nicht über die Option repositoryImplementationPostfix, +1 –

+0

Sie können tun, wie' ', wenn Sie sind XML-basierte Konfiguration verwenden. Weitere Informationen hierzu finden Sie in der [Referenzdokumentation] (http://static.springsource.org/spring-data/data-jpa/docs/current/reference/html/repositories.html#repositories.single-repository-behaviour). – TheKojuEffect

Verwandte Themen