当前位置: 首页 > 知识库问答 >
问题:

Spring data elasticsearch CRUD配置

梁烨
2023-03-14

我在配置spring data elasticsearch时遇到了问题,我遵循了这里提到的Crud存储库spring bean配置的过程。但我得到了错误:

线程“main”org.springframework.beans.factory.beanCreationException中出现异常:创建名为“customer service”的bean时出错:注入资源依赖项失败;嵌套异常为org.springframework.beans.factory.BeanCreationException:创建名为“Customer Repo”的bean时出错:设置bean属性“elasticsearch operations”时无法解析对bean“elasticsearch template”的引用;嵌套异常为org.springframework.beans.factory.BeanCreationException:创建类路径资源[spring-repository.xml]中定义的名为'elasticsearch template'的bean时出错:bean实例化失败;嵌套异常为org.springframework.beans.beanInstantiationException:无法实例化bean类[org.springframework.data.elasticsearch.core.elasticsearchtemplate]:构造函数引发异常;嵌套异常为java.lang.NosuchMethodError:com.fasterxml.jackson.core.jsonFactory.RequiresPropertyRerding()Z

代码如下:

CustomerService.java

@Service
public class CustomerService {
@Resource
CustomerRepo custRepo;

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

customer.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;
}
}
public interface CustomerRepo  extends ElasticsearchRepository<Customer, String> {
}
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);
}
}
<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

<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" />

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

共有1个答案

艾灿
2023-03-14

修改了这些文件后,它终于起作用了:

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。As 9200用于http,其中As 9300用于节点到节点通信。

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

 类似资料:
  • exVim 的配色由三部分组成: 你自己的Vim配色, exVim 插件的语法高亮和插件的配色. 你可以按照以下步骤来定制你的配色: 安装你的配色 exVim 提供了三种方法安装你的自定义配色 方法1. 在 ex-colorscheme 中安装(推荐) 首选的方法是在 ex-colorschemes 中安装自己的配色, 这种方法仅仅需要你把自己的配色文件放到 vimfiles/bundle/ex-

  • 目录: 在配置项目yml文件中: 问题: null 客户端YML: 有没有人知道我怎样才能在这两种情况下只带一个配置文件?

  • 丰富的过滤器插件的存在是 logstash 威力如此强大的重要因素。名为过滤器,其实提供的不单单是过滤的功能。在本章我们就会重点介绍几个插件,它们扩展了进入过滤器的原始数据,进行复杂的逻辑处理,甚至可以无中生有的添加新的 logstash 事件到后续的流程中去!

  • Codec 是 logstash 从 1.3.0 版开始新引入的概念(Codec 来自 Coder/decoder 两个单词的首字母缩写)。 在此之前,logstash 只支持纯文本形式输入,然后以过滤器处理它。但现在,我们可以在输入 期处理不同类型的数据,这全是因为有了 codec 设置。 所以,这里需要纠正之前的一个概念。Logstash 不只是一个input | filter | outpu

  • 在 “Hello World” 示例中,我们已经见到并介绍了 logstash 的运行流程和配置的基础语法。从这章开始,我们就要逐一介绍 logstash 流程中比较常用的一些插件,并在介绍中针对其主要适用的场景,推荐的配置,作一些说明。 限于篇幅,接下来内容中,配置示例不一定能贴完整。请记住一个原则:Logstash 配置一定要有一个 input 和一个 output。在演示过程中,如果没有写明

  • 根据文档--不管应用程序名称如何,如果模式与*/development(即localhost:8888/user/development或localhost:8888/demo/development)匹配,配置服务器应该匹配配置文件模式并获取适当的属性。例如:http://localhost:8888/demo/development我应该从ssh://git@xxxgithub.com/dev