Próbuję zaimplementować niestandardowe zachowanie do metody w repozytorium przy użyciu spring-data-jpa
.Implementowanie niestandardowego zachowania do metody repozytorium w danych wiosna-jpa
W ProductRepository
interfejsy jest
@Repository
public interface ProductRepository extends JpaRepository,
ProductRepositoryCustom {
public List findByProductName(String productName);
}
Interfejs ProductRepositoryCustom
zawierać saveCustom
do którego chcę wdrożyć niestandardowe zachowanie.
@Repository
public interface ProductRepositoryCustom {
public Product saveCustom(Product product);
}
To jest implementacja interfejsu ProductRepositoryCustom
. Metoda saveCustom
jest tutaj tylko przykładem. To, co naprawdę chcę zrobić, to zdefiniować niestandardową metodę, która zawiera serię instrukcji dotyczących metod rdzeniowych JpaRepository
. W tym celu próbowałem wstrzyknąć instancje ProductRepository
, ale dostałem błędy, jak pokazano poniżej. Jest to prosta aplikacja 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);
}
}
To StackTrace programu podczas uruchamiania 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)
Co mogę zrobić, aby wdrożyć niestandardowe zachowanie jak saveCustom
?
Dzięki bardzo, czuję się głupio nie spróbować usunięcie że '@ Repository' adnotacji, LOL. I o tym drugim problemie, napotkałem ten problem wcześniej niż ten pierwszy i rozwiązałem go po około godzinie frustracji wykonując jakąś modyfikację mojej konfiguracji 'ApplicationContext' przy użyciu' @EnableJpaRepositories (basePackages = "packages.repo", reositoryImplementationPostfix = "CustomImpl") ' – TheKojuEffect
nie wiedział o opcji reositoryImplementationPostfix, +1 –
Możesz zrobić jako' 'jeśli jesteś za pomocą konfiguracji opartej na XML. Więcej informacji można znaleźć w [Dokumentacja referencyjna] (http://static.springsource.org/spring-data/data-jpa/docs/current/reference/html/repositories.html#repositories.single-repository-behavour). –
TheKojuEffect