Skip to content

Commit 97bcf28

Browse files
kevinleturcakonczak
authored andcommitted
DATAES-135 - Add a flag in configuration to consider nested repositories
1 parent 9569442 commit 97bcf28

File tree

3 files changed

+85
-3
lines changed

3 files changed

+85
-3
lines changed

src/main/java/org/springframework/data/elasticsearch/repository/config/EnableElasticsearchRepositories.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013 the original author or authors.
2+
* Copyright 2013-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,6 +29,7 @@
2929
*
3030
* @author Rizwan Idrees
3131
* @author Mohsin Husen
32+
* @author Kevin Leturc
3233
*/
3334
@Target(ElementType.TYPE)
3435
@Retention(RetentionPolicy.RUNTIME)
@@ -111,4 +112,10 @@
111112
* @return
112113
*/
113114
String elasticsearchTemplateRef() default "elasticsearchTemplate";
115+
116+
/**
117+
* Configures whether nested repository-interfaces (e.g. defined as inner classes) should be discovered by the
118+
* repositories infrastructure.
119+
*/
120+
boolean considerNestedRepositories() default false;
114121
}

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013 the original author or authors.
2+
* Copyright 2013-15 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,15 +29,18 @@
2929
import org.springframework.context.annotation.Configuration;
3030
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
3131
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
32+
import org.springframework.data.elasticsearch.entities.SampleEntity;
3233
import org.springframework.data.elasticsearch.repositories.sample.SampleElasticsearchRepository;
3334
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
3435
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
36+
import org.springframework.data.repository.Repository;
3537
import org.springframework.test.context.ContextConfiguration;
3638
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
3739

3840
/**
3941
* @author Rizwan Idrees
4042
* @author Mohsin Husen
43+
* @author Kevin Leturc
4144
*/
4245
@RunWith(SpringJUnit4ClassRunner.class)
4346
@ContextConfiguration
@@ -51,7 +54,8 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
5154
}
5255

5356
@Configuration
54-
@EnableElasticsearchRepositories(basePackages = "org.springframework.data.elasticsearch.repositories.sample")
57+
@EnableElasticsearchRepositories(basePackages = {"org.springframework.data.elasticsearch.repositories.sample",
58+
"org.springframework.data.elasticsearch.config"})
5559
static class Config {
5660

5761
@Bean
@@ -63,6 +67,11 @@ public ElasticsearchOperations elasticsearchTemplate() {
6367
@Autowired
6468
private SampleElasticsearchRepository repository;
6569

70+
@Autowired(required = false)
71+
private SampleRepository nestedRepository;
72+
73+
interface SampleRepository extends Repository<SampleEntity, Long> {};
74+
6675
@Test
6776
public void bootstrapsRepository() {
6877
assertThat(repository, is(notNullValue()));
@@ -79,4 +88,9 @@ public void shouldScanSelectedPackage() {
7988
assertThat(beanNamesForType.length, is(1));
8089
assertThat(beanNamesForType[0], is("sampleElasticsearchRepository"));
8190
}
91+
92+
@Test
93+
public void hasNotNestedRepository() {
94+
assertNull(nestedRepository);
95+
}
8296
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2015 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+
* http://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 org.junit.Test;
19+
import org.junit.runner.RunWith;
20+
import org.springframework.beans.factory.annotation.Autowired;
21+
import org.springframework.context.annotation.Bean;
22+
import org.springframework.context.annotation.Configuration;
23+
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
24+
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
25+
import org.springframework.data.elasticsearch.entities.SampleEntity;
26+
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
27+
import org.springframework.data.repository.Repository;
28+
import org.springframework.test.context.ContextConfiguration;
29+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
30+
31+
import static org.elasticsearch.node.NodeBuilder.nodeBuilder;
32+
import static org.junit.Assert.assertNotNull;
33+
34+
/**
35+
* @author Kevin Leturc
36+
*/
37+
@RunWith(SpringJUnit4ClassRunner.class)
38+
@ContextConfiguration
39+
public class EnableNestedElasticsearchRepositoriesTests {
40+
41+
@Configuration
42+
@EnableElasticsearchRepositories(basePackages = {"org.springframework.data.elasticsearch.repositories.sample",
43+
"org.springframework.data.elasticsearch.config"}, considerNestedRepositories = true)
44+
static class Config {
45+
46+
@Bean
47+
public ElasticsearchOperations elasticsearchTemplate() {
48+
return new ElasticsearchTemplate(nodeBuilder().local(true).clusterName("testCluster2").node().client());
49+
}
50+
}
51+
52+
@Autowired(required = false)
53+
private SampleRepository nestedRepository;
54+
55+
interface SampleRepository extends Repository<SampleEntity, Long> {};
56+
57+
@Test
58+
public void hasNestedRepository() {
59+
assertNotNull(nestedRepository);
60+
}
61+
}

0 commit comments

Comments
 (0)