【发布时间】:2021-12-22 18:58:17
【问题描述】:
我正在将我们的一个应用程序从 spring-boot 版本 2.1.3.RELEASE 升级到 2.2.0.RELEASE,但我在使用 Elasticsearch 时遇到了一些问题。这是我得到的错误:
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'customCdrElasticConfig': Unsatisfied dependency expressed through field 'elasticsearchTemplate'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.data.elasticsearch.core.ElasticsearchTemplate' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=elasticsearchTemplate)}
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.data.elasticsearch.core.ElasticsearchTemplate' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=elasticsearchTemplate)}
代码:
package com.codex.reporting.config;
import com.codex.reporting.elasticsearch.service.impl.CustomElasticsearchTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
@Configuration
public class CustomCdrElasticConfig {
@Autowired
@Qualifier("elasticsearchTemplate")
private ElasticsearchTemplate elasticsearchTemplate;
@Bean
public CustomElasticsearchTemplate customElasticsearchTemplate(){
return new CustomElasticsearchTemplate(elasticsearchTemplate.getClient());
}
}
pom.xml
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>x-pack-transport</artifactId>
<version>6.8.13</version>
</dependency>
<!-- Dependency for high level rest client, for Rest Client -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>6.8.13</version>
</dependency>
<!-- For Rest High Level Client -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.8.13</version>
</dependency>
<!-- Dependency for high level rest client, for elasticsearch Core -->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch-core</artifactId>
<version>6.8.13</version>
</dependency>
【问题讨论】:
-
我在 Spring Boot 项目的 POM 文件中检查了 here,并验证了
6.8.13是使用 Spring Boot2.2.x时 ElasticSearch 的正确版本。 -
"ElasticsearchTemplate" (org.springframework.data.elasticsearch.core.ElasticsearchTemplate) 是 Spring Data Elasticsearch 的一部分,我在您共享的 pom 中看不到这种依赖关系。
-
@Shailendra :抱歉错过了之前在问题中添加的内容,它已经存在于 pom.xml 中。我使用的是 3.2.3.RELEASE 版本。
标签: spring-boot elasticsearch spring-data-elasticsearch