From 786f025818f8b8d5fb5bda16fe169024e8b6bc24 Mon Sep 17 00:00:00 2001 From: Artur Konczak Date: Sat, 29 Aug 2015 02:31:17 +0100 Subject: [PATCH] Auto-configure Elasticsearch converter and context Extend ElasticsearchDataAutoConfiguration to also configure an ElasticsearchConverter and SimpleElasticsearchMappingContext both of which are required for Spring Data REST. Closes gh-3847 --- .../ElasticsearchDataAutoConfiguration.java | 26 ++++++++++++++-- ...asticsearchDataAutoConfigurationTests.java | 31 +++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/elasticsearch/ElasticsearchDataAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/elasticsearch/ElasticsearchDataAutoConfiguration.java index f09207811f..9ac3908ebc 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/elasticsearch/ElasticsearchDataAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/elasticsearch/ElasticsearchDataAutoConfiguration.java @@ -25,6 +25,9 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; +import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter; +import org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter; +import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext; import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories; /** @@ -46,8 +49,27 @@ public class ElasticsearchDataAutoConfiguration { @Bean @ConditionalOnMissingBean - public ElasticsearchTemplate elasticsearchTemplate(Client client) { - return new ElasticsearchTemplate(client); + public ElasticsearchTemplate elasticsearchTemplate(Client client, + ElasticsearchConverter converter) { + try { + return new ElasticsearchTemplate(client, converter); + } + catch (Exception ex) { + throw new IllegalStateException(ex); + } + } + + @Bean + @ConditionalOnMissingBean + public ElasticsearchConverter elasticsearchConverter( + SimpleElasticsearchMappingContext mappingContext) { + return new MappingElasticsearchConverter(mappingContext); + } + + @Bean + @ConditionalOnMissingBean + public SimpleElasticsearchMappingContext mappingContext() { + return new SimpleElasticsearchMappingContext(); } } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/elasticsearch/ElasticsearchDataAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/elasticsearch/ElasticsearchDataAutoConfigurationTests.java index 1ee34c2e7f..0aa14bc9cd 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/elasticsearch/ElasticsearchDataAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/elasticsearch/ElasticsearchDataAutoConfigurationTests.java @@ -23,6 +23,8 @@ import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfigurati import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; +import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter; +import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext; import static org.junit.Assert.assertEquals; @@ -30,6 +32,7 @@ import static org.junit.Assert.assertEquals; * Tests for {@link ElasticsearchDataAutoConfiguration}. * * @author Phillip Webb + * @author Artur Konczak */ public class ElasticsearchDataAutoConfigurationTests { @@ -56,4 +59,32 @@ public class ElasticsearchDataAutoConfigurationTests { this.context.getBeanNamesForType(ElasticsearchTemplate.class).length); } + @Test + public void mappingContextExists() { + this.context = new AnnotationConfigApplicationContext(); + EnvironmentTestUtils.addEnvironment(this.context, + "spring.data.elasticsearch.properties.path.data:target/data", + "spring.data.elasticsearch.properties.path.logs:target/logs"); + this.context.register(PropertyPlaceholderAutoConfiguration.class, + ElasticsearchAutoConfiguration.class, + ElasticsearchDataAutoConfiguration.class); + this.context.refresh(); + assertEquals(1, this.context + .getBeanNamesForType(SimpleElasticsearchMappingContext.class).length); + } + + @Test + public void converterExists() { + this.context = new AnnotationConfigApplicationContext(); + EnvironmentTestUtils.addEnvironment(this.context, + "spring.data.elasticsearch.properties.path.data:target/data", + "spring.data.elasticsearch.properties.path.logs:target/logs"); + this.context.register(PropertyPlaceholderAutoConfiguration.class, + ElasticsearchAutoConfiguration.class, + ElasticsearchDataAutoConfiguration.class); + this.context.refresh(); + assertEquals(1, + this.context.getBeanNamesForType(ElasticsearchConverter.class).length); + } + }