Skip to content

Commit 873dcca

Browse files
gpantheakonczak
authored andcommitted
DATAES-93 Make fully ApplicationContextAware, allows bean resolvers.
1 parent a92af0e commit 873dcca

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@
7171
import org.elasticsearch.search.sort.SortOrder;
7272
import org.slf4j.Logger;
7373
import org.slf4j.LoggerFactory;
74+
import org.springframework.beans.BeansException;
75+
import org.springframework.context.ApplicationContext;
76+
import org.springframework.context.ApplicationContextAware;
7477
import org.springframework.core.io.ClassPathResource;
7578
import org.springframework.data.domain.Page;
7679
import org.springframework.data.domain.Sort;
@@ -95,7 +98,7 @@
9598
* @author Artur Konczak
9699
*/
97100

98-
public class ElasticsearchTemplate implements ElasticsearchOperations {
101+
public class ElasticsearchTemplate implements ElasticsearchOperations, ApplicationContextAware {
99102

100103
private static final Logger logger = LoggerFactory.getLogger(ElasticsearchTemplate.class);
101104
private Client client;
@@ -120,8 +123,8 @@ public ElasticsearchTemplate(Client client, ElasticsearchConverter elasticsearch
120123

121124
public ElasticsearchTemplate(Client client, ElasticsearchConverter elasticsearchConverter, ResultsMapper resultsMapper) {
122125
this.client = client;
123-
this.elasticsearchConverter = (elasticsearchConverter == null) ? new MappingElasticsearchConverter(
124-
new SimpleElasticsearchMappingContext()) : elasticsearchConverter;
126+
this.elasticsearchConverter = (elasticsearchConverter == null) ? new MappingElasticsearchConverter(
127+
new SimpleElasticsearchMappingContext()) : elasticsearchConverter;
125128
this.resultsMapper = (resultsMapper == null) ? new DefaultResultMapper(this.elasticsearchConverter.getMappingContext()) : resultsMapper;
126129
}
127130

@@ -873,7 +876,14 @@ private List<String> extractIds(SearchResponse response) {
873876
return ids;
874877
}
875878

876-
private static String[] toArray(List<String> values) {
879+
@Override
880+
public void setApplicationContext(ApplicationContext context) throws BeansException {
881+
if(elasticsearchConverter instanceof ApplicationContextAware){
882+
((ApplicationContextAware)elasticsearchConverter).setApplicationContext(context);
883+
}
884+
}
885+
886+
private static String[] toArray(List<String> values) {
877887
String[] valuesAsArray = new String[values.size()];
878888
return values.toArray(valuesAsArray);
879889
}

src/main/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,8 @@ public ConversionService getConversionService() {
6161
@Override
6262
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
6363
this.applicationContext = applicationContext;
64+
if(mappingContext instanceof ApplicationContextAware){
65+
((ApplicationContextAware)mappingContext).setApplicationContext(applicationContext);
66+
}
6467
}
6568
}

src/main/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchMappingContext.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
import java.beans.PropertyDescriptor;
1919
import java.lang.reflect.Field;
2020

21+
import org.springframework.beans.BeansException;
22+
import org.springframework.context.ApplicationContext;
23+
import org.springframework.context.ApplicationContextAware;
2124
import org.springframework.data.mapping.context.AbstractMappingContext;
2225
import org.springframework.data.mapping.model.SimpleTypeHolder;
2326
import org.springframework.data.util.TypeInformation;
@@ -30,16 +33,29 @@
3033
*/
3134

3235
public class SimpleElasticsearchMappingContext extends
33-
AbstractMappingContext<SimpleElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> {
36+
AbstractMappingContext<SimpleElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> implements ApplicationContextAware{
3437

35-
@Override
38+
private ApplicationContext context;
39+
40+
@Override
3641
protected <T> SimpleElasticsearchPersistentEntity<?> createPersistentEntity(TypeInformation<T> typeInformation) {
37-
return new SimpleElasticsearchPersistentEntity<T>(typeInformation);
42+
final SimpleElasticsearchPersistentEntity<T> persistentEntity =
43+
new SimpleElasticsearchPersistentEntity<T>(typeInformation);
44+
if(context != null)
45+
{
46+
persistentEntity.setApplicationContext(context);
47+
}
48+
return persistentEntity;
3849
}
3950

4051
@Override
4152
protected ElasticsearchPersistentProperty createPersistentProperty(Field field, PropertyDescriptor descriptor,
4253
SimpleElasticsearchPersistentEntity<?> owner, SimpleTypeHolder simpleTypeHolder) {
4354
return new SimpleElasticsearchPersistentProperty(field, descriptor, owner, simpleTypeHolder);
4455
}
56+
57+
@Override
58+
public void setApplicationContext(ApplicationContext context) throws BeansException {
59+
this.context = context;
60+
}
4561
}

0 commit comments

Comments
 (0)