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());
}
全部代碼文章來源:http://www.zghlxwxcb.cn/news/detail-507818.html
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)!