1

我在配置 spring data elasticsearch 时遇到问题,我按照此处提到的过程Spring bean configuration for Crud Repositories。但我收到错误:

线程“主”org.springframework.beans.factory.BeanCreationException 中的异常:创建名为“customerService”的 bean 时出错:资源依赖项注入失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“customerRepo”的 bean 时出错:设置 bean 属性“elasticsearchOperations”时无法解析对 bean“elasticsearchTemplate”的引用;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建类路径资源 [spring-repository.xml] 中定义的名称为“elasticsearchTemplate”的 bean 时出错:bean 的实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 bean 类 [org.springframework.data.elasticsearch.core. ElasticsearchTemplate]:构造函数抛出异常;嵌套异常是 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;
}
}

客户回购.java

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

主类.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);
}
}

弹簧客户.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

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

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

4

1 回答 1

1

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

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 用于节点到节点通信。

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

于 2015-04-24T08:15:19.503 回答