में एक रिपोजिटरी विधि में कस्टम व्यवहार को कार्यान्वित करें मैं 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
तरह कस्टम व्यवहार लागू करने के लिए कर सकते हैं?
इस घोषणा से @Repository टिप्पणी निकालने:
धन्यवाद बहुत ज्यादा है कि '@ Repository' एनोटेशन, LOL। और वह दूसरी समस्या के बारे में, मुझे लगता है कि समस्या यह है कि पहले एक से पहले का सामना करना पड़ा और हताशा के लगभग एक घंटे बाद इसे हल '@EnableJpaRepositories (basePackages =" packages.repo "का उपयोग करके अपनी' ApplicationContext' config करने के लिए कुछ अनुकूलन कर दिया है, repositoryImplementationPostfix = "CustomImpl") ' – TheKojuEffect
repositoryImplementationPostfix विकल्प के बारे में पता नहीं था, +1 –
आप क्या कर सकते हैं के रूप में' <खजाने आधार पैकेज = "packages.repo" भंडार-impl-पोस्टफ़िक्स = "CustomImpl" /> 'अगर आप एक्सएमएल आधारित विन्यास का उपयोग कर। अधिक विस्तार के लिए इस [संदर्भ प्रलेखन] (http://static.springsource.org/spring-data/data-jpa/docs/current/reference/html/repositories.html#repositories.single-repository-behaviour) देखें। – TheKojuEffect