From d92e7feabca751edb548a645858508f814e6e7d8 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Tue, 16 Jun 2020 15:16:29 +0200 Subject: [PATCH] #509 - Favor Spring Boot auto-configuration in Elasticsearch example. --- elasticsearch/example/README.md | 2 +- .../conference/ApplicationConfiguration.java | 13 +++++------ .../src/main/resources/application.properties | 4 +--- .../ElasticsearchOperationsTest.java | 9 ++++---- elasticsearch/reactive/README.md | 16 +++++--------- .../conference/ApplicationConfiguration.java | 22 +++++-------------- .../src/main/resources/application.properties | 4 +--- .../ReactiveElasticsearchOperationsTest.java | 7 +++--- elasticsearch/rest/README.md | 13 ++++------- .../conference/ApplicationConfiguration.java | 19 +++++----------- .../src/main/resources/application.properties | 3 +-- 11 files changed, 37 insertions(+), 75 deletions(-) diff --git a/elasticsearch/example/README.md b/elasticsearch/example/README.md index b898f615..001c8bb7 100644 --- a/elasticsearch/example/README.md +++ b/elasticsearch/example/README.md @@ -12,5 +12,5 @@ Requirements: To use local elasticsearch cluster: * install elasticsearch -* uncomment both properties in file 'application.properties' +* Adapt 'application.properties' to point to your elasticsearch cluster diff --git a/elasticsearch/example/src/main/java/example/springdata/elasticsearch/conference/ApplicationConfiguration.java b/elasticsearch/example/src/main/java/example/springdata/elasticsearch/conference/ApplicationConfiguration.java index c504ad2f..dcc4aaad 100644 --- a/elasticsearch/example/src/main/java/example/springdata/elasticsearch/conference/ApplicationConfiguration.java +++ b/elasticsearch/example/src/main/java/example/springdata/elasticsearch/conference/ApplicationConfiguration.java @@ -1,11 +1,11 @@ /* - * Copyright 2014-2018 the original author or authors. + * Copyright 2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * https://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -22,22 +22,21 @@ import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import org.elasticsearch.client.Client; + import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; import org.springframework.data.elasticsearch.client.NodeClientFactoryBean; import org.springframework.data.elasticsearch.core.ElasticsearchOperations; import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; import org.springframework.data.elasticsearch.core.geo.GeoPoint; -import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories; /** * @author Artur Konczak * @author Oliver Gierke * @author Christoph Strobl */ -@Configuration -@EnableElasticsearchRepositories +@SpringBootApplication class ApplicationConfiguration { @Autowired ElasticsearchOperations operations; @@ -56,7 +55,7 @@ class ApplicationConfiguration { } @Bean - public ElasticsearchTemplate elasticsearchTemplate(Client client) throws Exception { + public ElasticsearchTemplate elasticsearchTemplate(Client client) { return new ElasticsearchTemplate(client); } diff --git a/elasticsearch/example/src/main/resources/application.properties b/elasticsearch/example/src/main/resources/application.properties index 1982f98a..e3aca775 100644 --- a/elasticsearch/example/src/main/resources/application.properties +++ b/elasticsearch/example/src/main/resources/application.properties @@ -1,3 +1 @@ -# Uncomment both entries to connect with local elasticsearch cluster -#spring.data.elasticsearch.clusterName=elasticsearch -#spring.data.elasticsearch.clusterNodes=localhost:9300 +#spring.elasticsearch.rest.uris=http://localhost:9200 diff --git a/elasticsearch/example/src/test/java/example/springdata/elasticsearch/conference/ElasticsearchOperationsTest.java b/elasticsearch/example/src/test/java/example/springdata/elasticsearch/conference/ElasticsearchOperationsTest.java index b606b95c..114dd81f 100644 --- a/elasticsearch/example/src/test/java/example/springdata/elasticsearch/conference/ElasticsearchOperationsTest.java +++ b/elasticsearch/example/src/test/java/example/springdata/elasticsearch/conference/ElasticsearchOperationsTest.java @@ -1,11 +1,11 @@ /* - * Copyright 2014-2018 the original author or authors. + * Copyright 2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * https://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -15,13 +15,14 @@ */ package example.springdata.elasticsearch.conference; +import static org.assertj.core.api.Assertions.*; import java.text.ParseException; import java.text.SimpleDateFormat; -import java.util.List; import org.junit.Test; import org.junit.runner.RunWith; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.elasticsearch.core.ElasticsearchOperations; @@ -33,8 +34,6 @@ import org.springframework.data.elasticsearch.core.query.Criteria; import org.springframework.data.elasticsearch.core.query.CriteriaQuery; import org.springframework.test.context.junit4.SpringRunner; -import static org.assertj.core.api.Assertions.*; - /** * Test case to show Spring Data Elasticsearch functionality. * diff --git a/elasticsearch/reactive/README.md b/elasticsearch/reactive/README.md index 1a27d07d..5f38d115 100644 --- a/elasticsearch/reactive/README.md +++ b/elasticsearch/reactive/README.md @@ -1,19 +1,13 @@ # Spring Data Elasticsearch - Reactive Examples -The `ReactiveElasticsearchClient` is a non official driver based on `WebClient`. +The `ReactiveElasticsearchClient` is a client based on `WebClient`. It uses the request/response objects provided by the Elasticsearch core project. Calls are directly operated on the reactive stack, not wrapping async (thread pool bound) responses into reactive types. -````java -class ApplicationConfiguration extends AbstractReactiveElasticsearchConfiguration { - - @Bean - @Override - public ReactiveElasticsearchClient reactiveElasticsearchClient() { - return ReactiveRestClients.create(ClientConfiguration.localhost()); - } -} -```` +```java +@SpringBootApplication +class ApplicationConfiguration {} +``` The `ReactiveElasticsearchClient` can be used with the `ReactiveElasticsearchOperations` and `ReactiveElasticsearchRepository`. diff --git a/elasticsearch/reactive/src/main/java/example/springdata/elasticsearch/conference/ApplicationConfiguration.java b/elasticsearch/reactive/src/main/java/example/springdata/elasticsearch/conference/ApplicationConfiguration.java index b3043168..33102785 100644 --- a/elasticsearch/reactive/src/main/java/example/springdata/elasticsearch/conference/ApplicationConfiguration.java +++ b/elasticsearch/reactive/src/main/java/example/springdata/elasticsearch/conference/ApplicationConfiguration.java @@ -1,11 +1,11 @@ /* - * Copyright 2019 the original author or authors. + * Copyright 2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * https://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -27,33 +27,21 @@ import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; import org.elasticsearch.client.RequestOptions; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; +import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.data.elasticsearch.client.ClientConfiguration; import org.springframework.data.elasticsearch.client.RestClients; -import org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient; -import org.springframework.data.elasticsearch.client.reactive.ReactiveRestClients; -import org.springframework.data.elasticsearch.config.AbstractReactiveElasticsearchConfiguration; import org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations; import org.springframework.data.elasticsearch.core.geo.GeoPoint; -import org.springframework.data.elasticsearch.repository.config.EnableReactiveElasticsearchRepositories; /** * @author Christoph Strobl */ -@Configuration -@EnableReactiveElasticsearchRepositories -class ApplicationConfiguration extends AbstractReactiveElasticsearchConfiguration { +@SpringBootApplication +class ApplicationConfiguration { @Autowired ReactiveElasticsearchOperations operations; @Autowired ConferenceRepository repository; - @Bean - @Override - public ReactiveElasticsearchClient reactiveElasticsearchClient() { - return ReactiveRestClients.create(ClientConfiguration.localhost()); - } - @PostConstruct public void insertDataSample() { diff --git a/elasticsearch/reactive/src/main/resources/application.properties b/elasticsearch/reactive/src/main/resources/application.properties index 1982f98a..f5dac6c0 100644 --- a/elasticsearch/reactive/src/main/resources/application.properties +++ b/elasticsearch/reactive/src/main/resources/application.properties @@ -1,3 +1 @@ -# Uncomment both entries to connect with local elasticsearch cluster -#spring.data.elasticsearch.clusterName=elasticsearch -#spring.data.elasticsearch.clusterNodes=localhost:9300 +spring.data.elasticsearch.client.reactive.endpoints=localhost:9200 diff --git a/elasticsearch/reactive/src/test/java/example/springdata/elasticsearch/conference/ReactiveElasticsearchOperationsTest.java b/elasticsearch/reactive/src/test/java/example/springdata/elasticsearch/conference/ReactiveElasticsearchOperationsTest.java index 788c950d..760c23c1 100644 --- a/elasticsearch/reactive/src/test/java/example/springdata/elasticsearch/conference/ReactiveElasticsearchOperationsTest.java +++ b/elasticsearch/reactive/src/test/java/example/springdata/elasticsearch/conference/ReactiveElasticsearchOperationsTest.java @@ -1,11 +1,11 @@ /* - * Copyright 2019 the original author or authors. + * Copyright 2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * https://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -32,13 +32,14 @@ import org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperatio import org.springframework.data.elasticsearch.core.query.Criteria; import org.springframework.data.elasticsearch.core.query.CriteriaQuery; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.junit4.SpringRunner; /** * Test case to show Spring Data Elasticsearch functionality. * * @author Christoph Strobl */ -@RunWith(SpringJUnit4ClassRunner.class) +@RunWith(SpringRunner.class) @SpringBootTest(classes = ApplicationConfiguration.class) public class ReactiveElasticsearchOperationsTest { diff --git a/elasticsearch/rest/README.md b/elasticsearch/rest/README.md index fb760a70..8b6711d9 100644 --- a/elasticsearch/rest/README.md +++ b/elasticsearch/rest/README.md @@ -1,15 +1,10 @@ # Spring Data Elasticsearch - High Level REST Client Examples -````java -class ApplicationConfiguration extends AbstractElasticsearchConfiguration { +```java +@SpringBootApplication +class ApplicationConfiguration {} +``` - @Bean - @Override - public RestHighLevelClient elasticsearchClient() { - return RestClients.create(ClientConfiguration.localhost()).rest(); - } -} -```` The `RestHighLevelClient` can be used with the `ElasticsearchOperations` and `ElasticsearchRepository`. diff --git a/elasticsearch/rest/src/main/java/example/springdata/elasticsearch/conference/ApplicationConfiguration.java b/elasticsearch/rest/src/main/java/example/springdata/elasticsearch/conference/ApplicationConfiguration.java index 62a78e7f..4a716ee9 100644 --- a/elasticsearch/rest/src/main/java/example/springdata/elasticsearch/conference/ApplicationConfiguration.java +++ b/elasticsearch/rest/src/main/java/example/springdata/elasticsearch/conference/ApplicationConfiguration.java @@ -1,11 +1,11 @@ /* - * Copyright 2014-2018 the original author or authors. + * Copyright 2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * https://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -20,12 +20,9 @@ import java.util.Arrays; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; -import org.elasticsearch.client.RestHighLevelClient; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Configuration; -import org.springframework.data.elasticsearch.client.ClientConfiguration; -import org.springframework.data.elasticsearch.client.RestClients; -import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration; import org.springframework.data.elasticsearch.core.ElasticsearchOperations; import org.springframework.data.elasticsearch.core.geo.GeoPoint; import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories; @@ -35,14 +32,8 @@ import org.springframework.data.elasticsearch.repository.config.EnableElasticsea * @author Oliver Gierke * @author Christoph Strobl */ -@Configuration -@EnableElasticsearchRepositories -class ApplicationConfiguration extends AbstractElasticsearchConfiguration { - - @Override - public RestHighLevelClient elasticsearchClient() { - return RestClients.create(ClientConfiguration.localhost()).rest(); - } +@SpringBootApplication +class ApplicationConfiguration { @Autowired ElasticsearchOperations elasticsearchOperations; @Autowired ConferenceRepository repository; diff --git a/elasticsearch/rest/src/main/resources/application.properties b/elasticsearch/rest/src/main/resources/application.properties index 1982f98a..b798c505 100644 --- a/elasticsearch/rest/src/main/resources/application.properties +++ b/elasticsearch/rest/src/main/resources/application.properties @@ -1,3 +1,2 @@ # Uncomment both entries to connect with local elasticsearch cluster -#spring.data.elasticsearch.clusterName=elasticsearch -#spring.data.elasticsearch.clusterNodes=localhost:9300 +spring.elasticsearch.rest.uris=http://localhost:9200