SpringBoot中构建带有含参构造函数的Bean,解决报错Parameter 0 of constructor in XXX required a bean ,elasticsearch继承AbstractElasticsearchConfiguration方法…

报错内容

Description:Parameter 0 of constructor in xxx...CommonElasticsearchRepository required a bean of type 'org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate' that could not be found.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' elasticsearchOperations and found beans named elasticsearchTemplateAction:Consider revisiting the entries above or defining a bean of type 'org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate' in your configuration.

报错信息是由于ElasticsearchRestTemplate中定义了含参数的构造函数,Spring自动构造和注入时未为该Bean传入参数,引起报错。

解决方案:使用@Bean注解手动创建ElasticsearchRestTemplate的实例,具体步骤如下:

在ElasticRestClientConfig extends AbstractElasticsearchConfiguration里面添加方法即可

    @Beanpublic ElasticsearchRestTemplate elasticsearchRestTemplate() {return new ElasticsearchRestTemplate(elasticsearchClient());}

全部代码

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;import java.time.Duration;
@Configuration
public class ElasticRestClientConfig extends AbstractElasticsearchConfiguration {@Value("${spring.elasticsearch.rest.uris}")private String url;@Value("${spring.elasticsearch.rest.username}")private String username;@Value("${spring.elasticsearch.rest.password}")private String password;@Override@Beanpublic RestHighLevelClient elasticsearchClient() {url = url.replace("http://", "");String[] urlArr = url.split(",");HttpHost[] httpPostArr = new HttpHost[urlArr.length];for (int i = 0; i < urlArr.length; i++) {HttpHost httpHost = new HttpHost(urlArr[i].split(":")[0].trim(),Integer.parseInt(urlArr[i].split(":")[1].trim()), "http");httpPostArr[i] = httpHost;}final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));RestClientBuilder builder = RestClient.builder(httpPostArr)// 异步httpclient配置.setHttpClientConfigCallback(httpClientBuilder -> {// 账号密码登录httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);// httpclient连接数配置httpClientBuilder.setMaxConnTotal(30);// 每个路由值指定最大连接数httpClientBuilder.setMaxConnPerRoute(10);// httpclient 设置保活策略|长时间不连接es,报错超时连接 每隔五分钟发送一次请求httpClientBuilder.setKeepAliveStrategy(((response, context) -> Duration.ofMinutes(5).toMillis()));return httpClientBuilder;});return new RestHighLevelClient(builder);}@Beanpublic ElasticsearchRestTemplate elasticsearchRestTemplate() {return new ElasticsearchRestTemplate(elasticsearchClient());}
}

参考文献:https://blog.csdn.net/egg1996911/article/details/80398847
参考文献:https://blog.csdn.net/qq_24950043/article/details/125578335

解决报错Parameter 0 of constructor in XXX required a bean...elasticsearch 继承ElasticsearchConfiguration方法相关推荐

  1. springCloud Feign调用报错 Parameter 6 of constructor in xxxxxxxx required a bean of type ‘xxxxxx‘ that

    Description: Parameter 6 of constructor in xxxxxxx required a bean of type 'xxxxxxx' that could not ...

  2. 解决:Parameter 0 of constructor in XXX required a bean of type ‘XXX‘ that could not be found

    解决:Parameter 0 of constructor in com.mise.smart.entity.HrmNoticeEntity required a bean of type 'java ...

  3. 解决:Parameter 0 of constructor in xxx required a bean of type ‘xxx‘ that could not be found.

    运行报错: Parameter 0 of constructor in com.xxx.Crawler required a bean of type 'java.lang.String' that ...

  4. Parameter 0 of constructor in XXX required a bean of type ‘XXX‘ that could not be found.

    今天在搭建微服务项目的时候遇到了一个问题 项目结构 实际的场景是OperationLogService需要注入OperationLogMapper,也就是最常见的业务层依赖数据层,但是项目启动的时候报 ...

  5. Parameter 1 of constructor in xxx required a bean of type xxx‘ that could not be found.已经解决

    使用Mybatis Plus 时遇到问题Parameter 1 of constructor in xxx required a bean of type xxx' that could not be ...

  6. springboot启动报错Parameter 0 of method a in com.* required a bean of type 'java.lang.String' that could

    springboot服务启动报错,报错信息如下 *************************** APPLICATION FAILED TO START ******************** ...

  7. Mybatis 报错`Parameter '0' not found. Available parameters are [arg1, arg0, param1, param2]`

    1.异常信息: Parameter '0' not found. Available parameters are [arg1, arg0, param1, param2] 这是因为mybatis的M ...

  8. spring boot报错:Parameter 4 of constructor in xx required a bean of type ‘xx‘ that could not be found.

    spring boot 项目启动服务时,报错: 根据报错提示,一直认为是 SpringBootApplication 没有扫描到对应的 bean 路径. 最后,仔细排查发现是粗心的问题,自定义的常量没 ...

  9. Springboot集成Swagger报错Parameter 0 of method linkDiscoverers in org.springframework.hateoas.config.Hat

    第一次尝试使用springboot完成后端开发,在做demo的过程中集成swagger发生报错. 其中,swagger版本为2.2.2,springboot版本为2.3.4 觉得可能是版本不匹配,将s ...

最新文章

  1. 基于TensorRT优化的Machine Translation
  2. c语言神州行用户,神州行焕新出发 你行我行神州行折射品牌自信心
  3. 八、redis性能测试
  4. Python:数组添加数据和删除数据
  5. 简单算法集锦(程序)
  6. 在安卓JNI/NDK中使用C++11
  7. 微软认证及课程简写含义
  8. maven配置报错以及The JAVA_HOME environment variable is not defined correctly的解决方法
  9. 计算机上怎么带源地址ping,怎么带源地址去ping服务器
  10. 一文让你读懂Macbook的使用技巧及命令
  11. Mybatis的xml映射文件,sql的模糊查询的实现(两种方式)占位符拼接,concat关键字
  12. c语言编译器好玩的代码,读懂这4个函数,528行代码,你也可以实现一个C语言编译器...
  13. java做报表_推荐6款常用的Java开源报表制作工具
  14. thinkpad sl400 换网卡,debian换驱动
  15. Cell Ranger count (gene expression) 输出文件解读
  16. 【免费分享】论文查重软件(亲测好用!)
  17. “智慧新电商”全渠道获客+智能服务接待
  18. 来客推电商|小程序+h5+app商城|含分销拼团砍价等多种引流插件|前后代码开源
  19. 光纤的模式-单模和多模
  20. aimesh r6400 开_适合家用的路由器有哪些?

热门文章

  1. Mac装Xcode后开不了机
  2. 平面设计矢量绘图软件 CorelDRAW 2019简体中文特别版 Win/Mac
  3. java数据查询_Java中的大量数据查询
  4. Android必备:签名整理
  5. 【K8S】k8s pv,pvc无法删除问题
  6. Java后端接收前端post方式传来的表单数据
  7. 外语学习的真实方法与误区18
  8. 高端生活变革之路:“速食文化”到“独享宴会”
  9. JAVA中字符串倒序、判断名字中英文、判断手机号格式以及正则表达式应用
  10. 使用电子签章确立区块链电子发票的法律效力