Spring数据elasticsearch CRUD配置 [英] Spring data elasticsearch CRUD configuration

查看:53
本文介绍了Spring数据elasticsearch CRUD配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在配置spring数据elasticsearch时遇到问题,我按照这里提到的程序Crud 存储库的 Spring bean 配置.但我收到错误:

I am facing problem while configuring the spring data elasticsearch, I followed the procedure mentioned here Spring bean configuration for Crud Repositories. But I am getting error:

线程main"中的异常org.springframework.beans.factory.BeanCreationException:错误创建名为customerService"的 bean:资源注入依赖失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:错误创建名为customerRepo"的 bean:无法解析对的引用设置 bean 属性时 bean 'elasticsearchTemplate''弹性搜索操作';嵌套异常是org.springframework.beans.factory.BeanCreationException:错误使用在类路径中定义的名称elasticsearchTemplate"创建 bean资源 [spring-repository.xml]:bean 实例化失败;嵌套例外是 org.springframework.beans.BeanInstantiationException:无法实例化 bean 类[org.springframework.data.elasticsearch.core.ElasticsearchTemplate]:构造函数抛出异常;嵌套异常是java.lang.NoSuchMethodError:com.fasterxml.jackson.core.JsonFactory.requiresPropertyOrdering()Z

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': Cannot resolve reference to bean 'elasticsearchTemplate' while setting bean property 'elasticsearchOperations'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'elasticsearchTemplate' defined in class path resource [spring-repository.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.data.elasticsearch.core.ElasticsearchTemplate]: Constructor threw exception; nested exception is java.lang.NoSuchMethodError: com.fasterxml.jackson.core.JsonFactory.requiresPropertyOrdering()Z

代码如下:

客户服务.java

@Service
public class CustomerService {
@Resource
CustomerRepo custRepo;

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

客户.java

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

@Id
private String id;
private String name;

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

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

public void setId(String id) {
    this.id = id;
}
}

CustomerRepo.java

CustomerRepo.java

public interface CustomerRepo  extends ElasticsearchRepository<Customer, String> {
}

MainClass.java

MainClass.java

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("test_name");
    cust.save(customer);
}
}

spring-customer.xml

spring-customer.xml

<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.xsd  http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.elasticsearch" />

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

</beans>

spring-repository.xml

spring-repository.xml

<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.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="xx.xx.xx.xx:9200" />

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

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

我不知道为什么它不起作用.请帮帮我.

I don't know why it is not working. Please help me out.

推荐答案

修改这些文件后终于成功了:

It worked finally, after modifying these files:

1) spring-customer.xml

1) spring-customer.xml

<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.xsd  http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.elasticsearch.repositories" />

<import resource="spring-repository.xml"/>
<bean id="customerService" class="com.elasticsearch.CustomerService" scope="prototype" >
        <property name="custRepo" ref="custRepo"></property>
</bean>
</beans>

2) 更改端口号.spring-repository.xml 中从 9200 到 9300.因为 9200 用于 http,而 9300 用于节点到节点通信.

2) Changing port no. from 9200 to 9300 in spring-repository.xml. As 9200 is for http, where as 9300 is for node to node communication.

3) 在 CustomerService.java 文件中为 custRepo 添加 getter 和 setter.

3) Adding getter and setter for custRepo in CustomerService.java file.

这篇关于Spring数据elasticsearch CRUD配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆