我在配置spring数据elasticsearch时遇到问题,我按照这里提到的程序Spring bean configuration for Crud Repositories进行了操作。但是我收到了错误:
线程中的异常" main" org.springframework.beans.factory.BeanCreationException:错误 创建名为' customerService':注入资源的bean 依赖失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 使用名称' customerRepo'创建bean:无法解析引用 bean' elasticsearchTemplate'设置bean属性时 ' elasticsearchOperations&#39 ;;嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 创建名为' elasticsearchTemplate'在类路径中定义 resource [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
以下是代码:
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;
}
}
CustomerRepo.java
public interface CustomerRepo extends ElasticsearchRepository<Customer, String> {
}
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);
}
}
弹簧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>
弹簧的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" />
我不知道为什么它不起作用。请帮帮我。
答案 0 :(得分:1)
在修改这些文件后,它最终起作用了:
1)spring-customer.xml
images
2)更改端口号spring-repository.xml中的9200到9300。因为9200用于http,其中9300用于节点到节点的通信。
3)在CustomerService.java文件中为custRepo添加getter和setter。