国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

解決報錯Parameter 0 of constructor in XXX required a bean...elasticsearch 繼承ElasticsearchConfiguration方法

這篇具有很好參考價值的文章主要介紹了解決報錯Parameter 0 of constructor in XXX required a bean...elasticsearch 繼承ElasticsearchConfiguration方法。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

SpringBoot中構(gòu)建帶有含參構(gòu)造函數(shù)的Bean,解決報錯Parameter 0 of constructor in XXX required a bean ,elasticsearch繼承AbstractElasticsearchConfiguration方法…

報錯內(nèi)容

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 elasticsearchTemplate

Action:

Consider revisiting the entries above or defining a bean of type 'org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate' in your configuration.

報錯信息是由于ElasticsearchRestTemplate中定義了含參數(shù)的構(gòu)造函數(shù),Spring自動構(gòu)造和注入時未為該Bean傳入?yún)?shù),引起報錯。

解決方案:使用@Bean注解手動創(chuàng)建ElasticsearchRestTemplate的實例,具體步驟如下:

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

    @Bean
    public 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
    @Bean
    public 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連接數(shù)配置
                    httpClientBuilder.setMaxConnTotal(30);
                    // 每個路由值指定最大連接數(shù)
                    httpClientBuilder.setMaxConnPerRoute(10);
                    // httpclient 設置?;畈呗裕L時間不連接es,報錯超時連接 每隔五分鐘發(fā)送一次請求
                    httpClientBuilder.setKeepAliveStrategy(((response, context) -> Duration.ofMinutes(5).toMillis()));
                    return httpClientBuilder;
                });
        return new RestHighLevelClient(builder);
    }

    @Bean
    public ElasticsearchRestTemplate elasticsearchRestTemplate() {
        return new ElasticsearchRestTemplate(elasticsearchClient());
    }
}

參考文獻:https://blog.csdn.net/egg1996911/article/details/80398847
參考文獻:https://blog.csdn.net/qq_24950043/article/details/125578335文章來源地址http://www.zghlxwxcb.cn/news/detail-507818.html

到了這里,關于解決報錯Parameter 0 of constructor in XXX required a bean...elasticsearch 繼承ElasticsearchConfiguration方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包