Spring boot required a bean named 'elasticsearchTemplate' that could not be found.

· 阿靠北啊 · 80阅读 · 2022-07-29

详细描述

spring boot 集成jpa与es,启动时出现required a bean named 'elasticsearchTemplate' that could not be found.

版本信息

spring boot 2.4.2

复现过程

Field template in com.rahul.es.api.service.QueryDSLService required a bean of type 'org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)

The following candidates were found but could not be injected:
    - Bean method 'elasticsearchTemplate' in 'ElasticsearchDataConfiguration.RestClientConfiguration' not loaded because @ConditionalOnMissingBean (names: elasticsearchTemplate types: org.springframework.data.elasticsearch.core.ElasticsearchOperations; SearchStrategy: all) found beans of type 'org.springframework.data.elasticsearch.core.ElasticsearchOperations' elasticsearchTemplate and found beans named elasticsearchTemplate




解决方案

注入以下bean即可

@Autowired
private RestHighLevelClient restHighLevelClient;


@Bean
public ElasticsearchRestTemplate restTemplate() throws Exception {
   return new ElasticsearchRestTemplate(restHighLevelClient);
}

@Bean
public ElasticsearchOperations elasticsearchTemplate() {
return new ElasticsearchRestTemplate(restHighLevelClient);
}

文章有用

已有 2人 推荐该文章,推荐越多越容易获得的官方扶持

微信扫码分享