5

Sto tentando di implementare un comportamento personalizzato su un metodo nel repository utilizzando spring-data-jpa.Implementare un comportamento personalizzato su un metodo di repository in spring-data-jpa

I ProductRepository interfacce è


@Repository 
public interface ProductRepository extends JpaRepository, 
     ProductRepositoryCustom { 

    public List findByProductName(String productName); 

} 

L'interfaccia ProductRepositoryCustom contengono una saveCustom a cui voglio implementare comportamento personalizzato.


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

Questa è l'implementazione per l'interfaccia ProductRepositoryCustom. Il metodo saveCustom qui è solo un esempio. Quello che voglio veramente fare è definire un metodo personalizzato tale che contenga una serie di istruzioni che coinvolgono i metodi core JpaRepository. Per questo ho provato a iniettare istanze ProductRepository ma ho ricevuto errori come mostrato di seguito.


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

} 

Questo è semplice ServerApp applicazione che ho eseguito.


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

Questa è la stacktrace del programma quando avvio il 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) 

Cosa posso fare per implementare un comportamento personalizzato come saveCustom?

risposta

9

Ci sono due problemi con le classi:

rimuova annotazione @Repository da questa dichiarazione:

@Repository 
public interface ProductRepositoryCustom { 

Questo risolverà il tuo problema attuale, ma apparirà another one.

La soluzione a questo è per rinominare

ProductRepositoryCustomImpl 

a

ProductRepositoryImpl 
+4

Grazie mille, mi sento stupido Non ho provato la rimozione che '@ Repository' annotazione, LOL. E riguardo a quel secondo problema, ho affrontato questo problema prima di quello primo e l'ho risolto dopo circa un'ora di frustrazione eseguendo alcune personalizzazioni alla mia configurazione 'ApplicationContext' usando' @EnableJpaRepositories (basePackages = "packages.repo", repositoryImplementationPostfix = "CustomImpl") ' – TheKojuEffect

+0

non sapeva sull'opzione repositoryImplementationPostfix, +1 –

+0

Puoi fare come' 'se sei usando la configurazione basata su XML. Fare riferimento a questo [Documentazione di riferimento] (http://static.springsource.org/spring-data/data-jpa/docs/current/reference/html/repositories.html#repositories.single-repository-behaviour) per ulteriori dettagli. – TheKojuEffect

Problemi correlati