From a73b7bb7ddd5393617f738aeacaa87689a744de6 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 8 Sep 2020 12:17:58 +0200 Subject: [PATCH] Polish This commit makes sure that no high-level client is auto-configured if a low-level client is registered as a bean. See gh-22358 --- .../ElasticsearchRestClientAutoConfiguration.java | 1 + .../ElasticsearchRestClientAutoConfigurationTests.java | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientAutoConfiguration.java index b77922f4a8..e30da5b261 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientAutoConfiguration.java @@ -50,6 +50,7 @@ import org.springframework.util.StringUtils; */ @Configuration(proxyBeanMethods = false) @ConditionalOnClass(RestHighLevelClient.class) +@ConditionalOnMissingBean(RestClient.class) @EnableConfigurationProperties(ElasticsearchRestClientProperties.class) public class ElasticsearchRestClientAutoConfiguration { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientAutoConfigurationTests.java index 3acf36d131..17e2339ac4 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientAutoConfigurationTests.java @@ -45,6 +45,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; /** * Tests for {@link ElasticsearchRestClientAutoConfiguration}. @@ -69,6 +70,13 @@ class ElasticsearchRestClientAutoConfigurationTests { .hasSingleBean(RestHighLevelClient.class)); } + @Test + void configureWhenCustomRestClientShouldBackOff() { + this.contextRunner.withBean("customRestClient", RestClient.class, () -> mock(RestClient.class)) + .run((context) -> assertThat(context).doesNotHaveBean(RestHighLevelClient.class) + .hasSingleBean(RestClient.class).hasBean("customRestClient")); + } + @Test void configureWhenCustomRestHighLevelClientShouldBackOff() { this.contextRunner.withUserConfiguration(CustomRestHighLevelClientConfiguration.class)