Skip to content

Commit deefeb4

Browse files
sothawomp911de
authored andcommitted
DATAES-563 - Add elasticsearchTemplate bean alias to AbstractElasticsearchConfiguration.
We now register ElasticsearchOperations through AbstractElasticsearchConfiguration with an additional bean alias to retain compatibility with existing code and to not require additional configuration when using @EnableElasticsearchRepositories. EnableElasticsearchRepositories defaults to a bean named elasticsearchTemplate while AbstractElasticsearchConfiguration exposed the template bean named elasticsearchOperations. Original pull request: spring-projects#271.
1 parent f0ae721 commit deefeb4

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

src/main/java/org/springframework/data/elasticsearch/config/AbstractElasticsearchConfiguration.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
/**
2424
* @author Christoph Strobl
25+
* @author Peter-Josef Meisch
2526
* @since 3.2
2627
* @see ElasticsearchConfigurationSupport
2728
*/
@@ -41,7 +42,7 @@ public abstract class AbstractElasticsearchConfiguration extends ElasticsearchCo
4142
*
4243
* @return never {@literal null}.
4344
*/
44-
@Bean
45+
@Bean(name = {"elasticsearchOperations", "elasticsearchTemplate"})
4546
public ElasticsearchOperations elasticsearchOperations() {
4647
return new ElasticsearchRestTemplate(elasticsearchClient(), elasticsearchConverter(), resultsMapper());
4748
}

src/test/java/org/springframework/data/elasticsearch/config/ElasticsearchConfigurationSupportUnitTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,20 @@ public void restConfigContainsElasticsearchTemplate() {
8585
assertThat(context.getBean(ElasticsearchRestTemplate.class)).isNotNull();
8686
}
8787

88+
@Test // DATAES-563
89+
public void restConfigContainsElasticsearchOperationsByName() {
90+
91+
AbstractApplicationContext context = new AnnotationConfigApplicationContext(RestConfig.class);
92+
assertThat(context.getBean("elasticsearchOperations")).isNotNull();
93+
}
94+
95+
@Test // DATAES-563
96+
public void restConfigContainsElasticsearchTemplateByName() {
97+
98+
AbstractApplicationContext context = new AnnotationConfigApplicationContext(RestConfig.class);
99+
assertThat(context.getBean("elasticsearchTemplate")).isNotNull();
100+
}
101+
88102
@Test // DATAES-504
89103
public void reactiveConfigContainsReactiveElasticsearchTemplate() {
90104

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.elasticsearch.config;
17+
18+
import static org.hamcrest.CoreMatchers.*;
19+
import static org.junit.Assert.*;
20+
import static org.mockito.Mockito.*;
21+
22+
import org.elasticsearch.client.RestHighLevelClient;
23+
import org.junit.Test;
24+
import org.junit.runner.RunWith;
25+
import org.springframework.beans.factory.annotation.Autowired;
26+
import org.springframework.context.annotation.Configuration;
27+
import org.springframework.data.elasticsearch.repositories.existing.index.CreateIndexFalseRepository;
28+
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
29+
import org.springframework.test.context.ContextConfiguration;
30+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
31+
32+
/**
33+
* @author Peter-Josef Meisch
34+
*/
35+
@RunWith(SpringJUnit4ClassRunner.class)
36+
@ContextConfiguration
37+
public class ElasticsearchConfigurationTests {
38+
39+
/*
40+
* using a repository with an entity that is set to createIndex = false as we have no elastic running for this test
41+
* and just check that all the necessary beans are created.
42+
*/
43+
@Autowired private CreateIndexFalseRepository repository;
44+
45+
@Configuration
46+
@EnableElasticsearchRepositories(
47+
basePackages = { "org.springframework.data.elasticsearch.repositories.existing.index",
48+
"org.springframework.data.elasticsearch.config" })
49+
static class Config extends AbstractElasticsearchConfiguration {
50+
51+
@Override
52+
public RestHighLevelClient elasticsearchClient() {
53+
return mock(RestHighLevelClient.class);
54+
}
55+
56+
}
57+
58+
@Test // DATAES-563
59+
public void bootstrapsRepository() {
60+
assertThat(repository, is(notNullValue()));
61+
}
62+
}

0 commit comments

Comments
 (0)