mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-14 08:02:11 +00:00
DATAES-136 - BACKPORT from master- added support for basePackage configuration for @EnableElasticsearchRepositories -
(cherry picked from commit fad9a56)
This commit is contained in:
parent
0aab5f940f
commit
7ec291d572
@ -15,8 +15,16 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.repository.config;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.core.annotation.AnnotationAttributes;
|
||||
import org.springframework.data.elasticsearch.annotations.Document;
|
||||
import org.springframework.data.elasticsearch.repository.ElasticsearchCrudRepository;
|
||||
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
|
||||
import org.springframework.data.elasticsearch.repository.support.ElasticsearchRepositoryFactoryBean;
|
||||
import org.springframework.data.repository.config.AnnotationRepositoryConfigurationSource;
|
||||
import org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport;
|
||||
@ -72,4 +80,22 @@ public class ElasticsearchRepositoryConfigExtension extends RepositoryConfigurat
|
||||
Element element = config.getElement();
|
||||
builder.addPropertyReference("elasticsearchOperations", element.getAttribute("elasticsearch-template-ref"));
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport#getIdentifyingAnnotations()
|
||||
*/
|
||||
@Override
|
||||
protected Collection<Class<? extends Annotation>> getIdentifyingAnnotations() {
|
||||
return Collections.<Class<? extends Annotation>>singleton(Document.class);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport#getIdentifyingTypes()
|
||||
*/
|
||||
@Override
|
||||
protected Collection<Class<?>> getIdentifyingTypes() {
|
||||
return Arrays.<Class<?>>asList(ElasticsearchRepository.class, ElasticsearchCrudRepository.class);
|
||||
}
|
||||
}
|
||||
|
@ -21,12 +21,16 @@ import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
|
||||
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
|
||||
import org.springframework.data.elasticsearch.repositories.sample.SampleElasticsearchRepository;
|
||||
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
|
||||
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
@ -37,7 +41,14 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration
|
||||
public class EnableElasticsearchRepositoriesTests {
|
||||
public class EnableElasticsearchRepositoriesTests implements ApplicationContextAware {
|
||||
|
||||
ApplicationContext context;
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
this.context = applicationContext;
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableElasticsearchRepositories(basePackages = "org.springframework.data.elasticsearch.repositories.sample")
|
||||
@ -56,4 +67,16 @@ public class EnableElasticsearchRepositoriesTests {
|
||||
public void bootstrapsRepository() {
|
||||
assertThat(repository, is(notNullValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldScanSelectedPackage() {
|
||||
//given
|
||||
|
||||
//when
|
||||
String[] beanNamesForType = context.getBeanNamesForType(ElasticsearchRepository.class);
|
||||
|
||||
//then
|
||||
assertThat(beanNamesForType.length, is(1));
|
||||
assertThat(beanNamesForType[0], is("sampleElasticsearchRepository"));
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user