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); + } + }