Crud 存储库的 Spring bean 配置

标签 spring dependency-injection elasticsearch spring-data spring-data-jpa

我正在开发一个将数据注入(inject) Elasticsearch 的应用程序。使用 spring-data-elastic-search jar 注入(inject)数据。

 package com.customer;

    import javax.annotation.Resource;

    import com.customer.repositories.CustomerRepo;

    @SuppressWarnings("restriction")
    public class CustomerService {

        @Resource
        CustomerRepo custRepo;

        public void save(Customer cust) {
            custRepo.save(cust);
        }
     }

==================================================== =================================
  package com.customer;

    import org.springframework.data.elasticsearch.annotations.Document;


    @Document(
        indexName = "Customer", type = "cust"
       )
    public class Customer{

        private String name;

        public Customer(String name) {
            this.name = name;
        }

        public String getName() {
            return this.name;
        }
    }

==================================================== =================================
  package com.customer.repositories;

    import com.customer.Customer;
    import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

    public interface CustomerRepo extends ElasticsearchRepository<Customer, String> {

    }

==================================================== =================================
 package com.customer;

    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;

    public class MainClass {  
        public static void main(String args[]) {
            ApplicationContext context = 
                new ClassPathXmlApplicationContext(new String[] {"spring-customer.xml"});
            CustomerService cust = (CustomerService)context.getBean("customerService");
            Customer customer = new Customer("appu");
            cust.save(customer);
        }

}

Spring-customer.xml
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.customer" />

    <import resource="spring-repository.xml"/>

     <bean id="customerService" class="com.customer.CustomerService" scope="prototype" >
        <property name="custRepo" ref="custRepo"></property>
     </bean>
</beans>

==================================================== =================================

Spring-repository.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
    xsi:schemaLocation="http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

    <elasticsearch:transport-client id="client" cluster-nodes="localhost:9300" />

    <bean name="elasticsearchTemplate"
        class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
        <constructor-arg name="client" ref="client" />
    </bean>

    <elasticsearch:repositories
        base-package="com.customer.repositories" />


The issue is that, I'm getting the following exception:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerRepo': FactoryBean threw exception on object creation; nested exception is java.lang.NullPointerException
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:306)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:313)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1105)
    at com.customer.MainClass.main(MainClass.java:12)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerRepo': FactoryBean threw exception on object creation; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:149)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:102)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1442)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:248)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:871)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:813)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:730)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:438)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:416)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:550)
    at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:155)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:92)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:303)
    ... 7 more
Caused by: java.lang.NullPointerException
    at org.springframework.data.elasticsearch.repository.support.MappingElasticsearchEntityInformation.<init>(MappingElasticsearchEntityInformation.java:57)
    at org.springframework.data.elasticsearch.repository.support.MappingElasticsearchEntityInformation.<init>(MappingElasticsearchEntityInformation.java:49)
    at org.springframework.data.elasticsearch.repository.support.ElasticsearchEntityInformationCreatorImpl.getEntityInformation(ElasticsearchEntityInformationCreatorImpl.java:46)
    at org.springframework.data.elasticsearch.repository.support.ElasticsearchRepositoryFactory.getEntityInformation(ElasticsearchRepositoryFactory.java:57)
    at org.springframework.data.elasticsearch.repository.support.ElasticsearchRepositoryFactory.getTargetRepository(ElasticsearchRepositoryFactory.java:64)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:147)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:162)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:44)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142)

我的 bean 配置似乎有些问题
 <bean id="customerService" class="com.customer.CustomerService" scope="prototype" >
    <property name="custRepo" ref="custRepo"></property>
 </bean>

CustomerRepository 是一个接口(interface),我如何强制 spring 自动查找实现类。

注意:当容器执行“ Autowiring ”时,这可以正常工作。但是在从 bean xml 文件初始化时,问题仍然存在。

任何人都可以帮助解决这个问题吗?

最佳答案

问题不在您的 bean 配置中,但您必须在 Customer 类中声明 @Id 如下

 @Document(
    indexName = "Customer", type = "cust"
   )
public class Customer{

    @Id
    private String id;
    private String name;

    //setters and getters

  }

您需要有 Id 才能索引实体,SD elasticsearch 不支持任何没有 id 的实体被索引。

编辑 1:

===========================

您不需要将服务定义为 bean 并手动将存储库注入(inject)其中。
只需用@Service 标记它并将存储库用作@Resource,如上所述

文件中的更改

1) 客户服务.java
包 com.customer;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.customer.repositories.CustomerRepo;

@SuppressWarnings("restriction")
@Service
public class CustomerService {

    @Resource
    CustomerRepo custRepo;

    public void save(Customer cust) {
        custRepo.save(cust);
    }
 }

2) spring-customer.xml
<?xml version="1.0" encoding="UTF-8"?>

      <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context.xsd">

      <context:component-scan base-package="com.customer" />

      <import resource="spring-repository.xml"/>

</beans>

我尚未对其进行测试,但应该可以按预期工作。

关于Crud 存储库的 Spring bean 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21132952/

相关文章:

mysql - 使用 Spring 的 "jdbc:initialize-database",如何使用存储过程运行脚本?

angular - 我应该如何从另一个 Injectable 扩展 Injectable,并在 angular2 中进行多次注入(inject)?

java - 洋葱-六边形架构依赖混淆

elasticsearch - Elasticsearch 高级客户端-如何使用发帖请求进行搜索?

ElasticSearch MapperParsingException 对象映射

spring - 使用 Spring 3.1 MVC 的上下文路径配置

java - 在 liferay 中使用 Spring MVC 和 Maven 自定义 Taglib?

spring - 实现驱动和依赖注入(inject)的好处与维护实现的成本

c# - 如何注册通用服务

python - Elasticsearch Python API 的简单查询结果为 "search() missing 1 required positional argument"