Skip to content

Commit 8be47bf

Browse files
committed
Improve framework for custom mapping couchbase converter.
1) have AbstractCouchbaseConverter.setCustomConversions call afterPropertiesSet() to register converters with conversionService 2) have MappingCouchbaseConverter constructor with mappingContext set its applicationContext from the applicationContext from mappingContext 3) add getApplicationContext() to CouchbaseMappingContext to use in setting applicationContext of MappingCouchbaseConverter 4) change examples in Config to show creating repository mappings using existing MappingCouchbaseConverter (which have customConversions / BeanNames.COUCHBASE_CUSTOM_CONVERSIONS and applicationContext) instead of creating their own and then adding the customConversion and applicationContext. 5) replace @Autowired couchbaseObjectMapper with couchbaseObjectMapper() to avoid the following when using @componentscan : Unsatisfied dependency expressed through field 'couchbaseObjectMapper'; nested exception is org.springframework.beans.factory. BeanCurrentlyInCreationException: Error creating bean with name 'couchbaseObjectMapper': Requested bean is currently in creation: Is there an unresolvable circular reference? 6) remove unused members fro StringBasedN1qlQueryParser Closes #1141.
1 parent d172f39 commit 8be47bf

File tree

6 files changed

+41
-26
lines changed

6 files changed

+41
-26
lines changed

src/main/java/org/springframework/data/couchbase/config/AbstractCouchbaseConfiguration.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors
2+
* Copyright 2012-2021 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.
@@ -49,7 +49,6 @@
4949
import org.springframework.util.StringUtils;
5050

5151
import com.couchbase.client.core.deps.com.fasterxml.jackson.databind.DeserializationFeature;
52-
import com.couchbase.client.core.deps.com.fasterxml.jackson.databind.Module;
5352
import com.couchbase.client.core.encryption.CryptoManager;
5453
import com.couchbase.client.core.env.Authenticator;
5554
import com.couchbase.client.core.env.PasswordAuthenticator;
@@ -60,7 +59,6 @@
6059
import com.couchbase.client.java.env.ClusterEnvironment;
6160
import com.couchbase.client.java.json.JacksonTransformers;
6261
import com.couchbase.client.java.json.JsonValueModule;
63-
import com.couchbase.client.java.json.RepackagedJsonValueModule;
6462
import com.fasterxml.jackson.databind.ObjectMapper;
6563

6664
/**
@@ -75,8 +73,6 @@
7573
@Configuration
7674
public abstract class AbstractCouchbaseConfiguration {
7775

78-
@Autowired ObjectMapper couchbaseObjectMapper;
79-
8076
/**
8177
* The connection string which allows the SDK to connect to the cluster.
8278
* <p>
@@ -144,7 +140,7 @@ public ClusterEnvironment couchbaseClusterEnvironment() {
144140
if (!nonShadowedJacksonPresent()) {
145141
throw new CouchbaseException("non-shadowed Jackson not present");
146142
}
147-
builder.jsonSerializer(JacksonJsonSerializer.create(couchbaseObjectMapper));
143+
builder.jsonSerializer(JacksonJsonSerializer.create(couchbaseObjectMapper()));
148144
configureEnvironment(builder);
149145
return builder.build();
150146
}

src/main/java/org/springframework/data/couchbase/core/convert/AbstractCouchbaseConverter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors
2+
* Copyright 2012-2021 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.
@@ -17,7 +17,6 @@
1717
package org.springframework.data.couchbase.core.convert;
1818

1919
import java.util.Collections;
20-
import java.util.Optional;
2120

2221
import org.springframework.beans.factory.InitializingBean;
2322
import org.springframework.core.convert.ConversionService;
@@ -74,6 +73,7 @@ public ConversionService getConversionService() {
7473
*/
7574
public void setCustomConversions(final CustomConversions conversions) {
7675
this.conversions = conversions;
76+
afterPropertiesSet(); // register the conversions with the conversion service
7777
}
7878

7979
/**

src/main/java/org/springframework/data/couchbase/core/convert/MappingCouchbaseConverter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors
2+
* Copyright 2012-2021 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.
@@ -148,6 +148,8 @@ public MappingCouchbaseConverter(
148148
this.mappingContext = mappingContext;
149149
typeMapper = new DefaultCouchbaseTypeMapper(typeKey != null ? typeKey : TYPEKEY_DEFAULT);
150150
spELContext = new SpELContext(CouchbaseDocumentPropertyAccessor.INSTANCE);
151+
// ApplicationContext is needed for environment for resolving SPEL expressions.
152+
this.setApplicationContext(((CouchbaseMappingContext) mappingContext).getApplicationContext());
151153
}
152154

153155
/**

src/main/java/org/springframework/data/couchbase/core/mapping/CouchbaseMappingContext.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors
2+
* Copyright 2012-2021 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.
@@ -112,6 +112,10 @@ public void setApplicationContext(final ApplicationContext applicationContext) t
112112
super.setApplicationContext(applicationContext);
113113
}
114114

115+
public ApplicationContext getApplicationContext() {
116+
return context;
117+
}
118+
115119
@Override
116120
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
117121
eventPublisher = applicationEventPublisher;

src/main/java/org/springframework/data/couchbase/repository/query/StringBasedN1qlQueryParser.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2020 the original author or authors.
2+
* Copyright 2017-2021 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.
@@ -125,9 +125,6 @@ public class StringBasedN1qlQueryParser {
125125
private final Collection<String> parameterNames = new HashSet<String>();
126126
public final N1QLExpression parsedExpression;
127127

128-
private GenericConversionService conversionService = new DefaultConversionService();
129-
private CustomConversions conversions = new CouchbaseCustomConversions(Collections.emptyList());
130-
131128
public StringBasedN1qlQueryParser(String statement, CouchbaseQueryMethod queryMethod, String bucketName,
132129
CouchbaseConverter couchbaseConverter, String typeField, String typeValue, ParameterAccessor accessor,
133130
SpelExpressionParser parser, QueryMethodEvaluationContextProvider evaluationContextProvider) {

src/test/java/org/springframework/data/couchbase/domain/Config.java

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@
1818

1919
import java.lang.reflect.InvocationTargetException;
2020

21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.context.ApplicationContext;
2123
import org.springframework.context.annotation.Bean;
2224
import org.springframework.context.annotation.Configuration;
2325
import org.springframework.data.auditing.DateTimeProvider;
26+
import org.springframework.data.convert.CustomConversions;
2427
import org.springframework.data.couchbase.CouchbaseClientFactory;
2528
import org.springframework.data.couchbase.SimpleCouchbaseClientFactory;
2629
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
@@ -33,6 +36,7 @@
3336
import org.springframework.data.couchbase.core.mapping.CouchbaseMappingContext;
3437
import org.springframework.data.couchbase.domain.time.AuditingDateTimeProvider;
3538
import org.springframework.data.couchbase.repository.auditing.EnableCouchbaseAuditing;
39+
import org.springframework.data.couchbase.repository.auditing.EnableReactiveCouchbaseAuditing;
3640
import org.springframework.data.couchbase.repository.config.EnableCouchbaseRepositories;
3741
import org.springframework.data.couchbase.repository.config.ReactiveRepositoryOperationsMapping;
3842
import org.springframework.data.couchbase.repository.config.RepositoryOperationsMapping;
@@ -48,7 +52,12 @@
4852
*/
4953
@Configuration
5054
@EnableCouchbaseRepositories
51-
@EnableCouchbaseAuditing(auditorAwareRef = "auditorAwareRef", dateTimeProviderRef = "dateTimeProviderRef")
55+
@EnableCouchbaseAuditing(auditorAwareRef = "auditorAwareRef", dateTimeProviderRef = "dateTimeProviderRef") // this
56+
// activates
57+
// auditing
58+
@EnableReactiveCouchbaseAuditing(auditorAwareRef = "reactiveAuditorAwareRef",
59+
dateTimeProviderRef = "dateTimeProviderRef") // this activates auditing
60+
5261
public class Config extends AbstractCouchbaseConfiguration {
5362
String bucketname = "travel-sample";
5463
String username = "Administrator";
@@ -104,6 +113,11 @@ public NaiveAuditorAware testAuditorAware() {
104113
return new NaiveAuditorAware();
105114
}
106115

116+
@Bean(name = "reactiveAuditorAwareRef")
117+
public ReactiveNaiveAuditorAware testReactiveAuditorAware() {
118+
return new ReactiveNaiveAuditorAware();
119+
}
120+
107121
@Bean(name = "dateTimeProviderRef")
108122
public DateTimeProvider testDateTimeProvider() {
109123
return new AuditingDateTimeProvider();
@@ -113,12 +127,12 @@ public DateTimeProvider testDateTimeProvider() {
113127
public void configureReactiveRepositoryOperationsMapping(ReactiveRepositoryOperationsMapping baseMapping) {
114128
try {
115129
// comment out references to 'protected' and 'mybucket' - they are only to show how multi-bucket would work
116-
// ReactiveCouchbaseTemplate personTemplate =
117-
// myReactiveCouchbaseTemplate(myCouchbaseClientFactory("protected"),new MappingCouchbaseConverter());
130+
// ReactiveCouchbaseTemplate personTemplate = myReactiveCouchbaseTemplate(myCouchbaseClientFactory("protected"),
131+
// (MappingCouchbaseConverter) (baseMapping.getDefault().getConverter()));
118132
// baseMapping.mapEntity(Person.class, personTemplate); // Person goes in "protected" bucket
119-
// ReactiveCouchbaseTemplate userTemplate = myReactiveCouchbaseTemplate(myCouchbaseClientFactory("mybucket"),new
120-
// MappingCouchbaseConverter());
121-
// baseMapping.mapEntity(User.class, userTemplate); // User goes in "mybucket"
133+
// ReactiveCouchbaseTemplate userTemplate = myReactiveCouchbaseTemplate(myCouchbaseClientFactory("mybucket"),
134+
// (MappingCouchbaseConverter) (baseMapping.getDefault().getConverter()));
135+
//baseMapping.mapEntity(User.class, userTemplate); // User goes in "mybucket"
122136
// everything else goes in getBucketName() ( which is travel-sample )
123137
} catch (Exception e) {
124138
throw e;
@@ -129,11 +143,12 @@ public void configureReactiveRepositoryOperationsMapping(ReactiveRepositoryOpera
129143
public void configureRepositoryOperationsMapping(RepositoryOperationsMapping baseMapping) {
130144
try {
131145
// comment out references to 'protected' and 'mybucket' - they are only to show how multi-bucket would work
132-
// CouchbaseTemplate personTemplate = myCouchbaseTemplate(myCouchbaseClientFactory("protected"),new
133-
// MappingCouchbaseConverter());
146+
// CouchbaseTemplate personTemplate = myCouchbaseTemplate(myCouchbaseClientFactory("protected"),
147+
// (MappingCouchbaseConverter) (baseMapping.getDefault().getConverter()));
134148
// baseMapping.mapEntity(Person.class, personTemplate); // Person goes in "protected" bucket
135-
// CouchbaseTemplate userTemplate = myCouchbaseTemplate(myCouchbaseClientFactory("mybucket"),new
136-
// MappingCouchbaseConverter());
149+
// MappingCouchbaseConverter cvtr = (MappingCouchbaseConverter)baseMapping.getDefault().getConverter();
150+
// CouchbaseTemplate userTemplate = myCouchbaseTemplate(myCouchbaseClientFactory("mybucket"),
151+
// (MappingCouchbaseConverter) (baseMapping.getDefault().getConverter()));
137152
// baseMapping.mapEntity(User.class, userTemplate); // User goes in "mybucket"
138153
// everything else goes in getBucketName() ( which is travel-sample )
139154
} catch (Exception e) {
@@ -176,10 +191,11 @@ public MappingCouchbaseConverter mappingCouchbaseConverter() {
176191
return converter;
177192
}
178193

194+
/* This uses a CustomMappingCouchbaseConverter instead of MappingCouchbaseConverter */
179195
@Override
180-
@Bean(name = "couchbaseMappingConverter")
196+
@Bean(name = "mappingCouchbaseConverter")
181197
public MappingCouchbaseConverter mappingCouchbaseConverter(CouchbaseMappingContext couchbaseMappingContext,
182-
CouchbaseCustomConversions couchbaseCustomConversions) {
198+
CouchbaseCustomConversions couchbaseCustomConversions /* there is a customConversions() method bean */) {
183199
// MappingCouchbaseConverter relies on a SimpleInformationMapper
184200
// that has an getAliasFor(info) that just returns getType().getName().
185201
// Our CustomMappingCouchbaseConverter uses a TypeBasedCouchbaseTypeMapper that will

0 commit comments

Comments
 (0)