Improve framework for custom mapping couchbase converter. (#1148)

1) 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.

2) 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?

3) remove unused members fro StringBasedN1qlQueryParser

Closes #1141.

Co-authored-by: mikereiche <michael.reiche@couchbase.com>
This commit is contained in:
Michael Reiche
2021-06-09 21:36:45 -04:00
committed by GitHub
parent d172f395a8
commit 67a7937cd2
4 changed files with 34 additions and 25 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors
* Copyright 2012-2021 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -49,7 +49,6 @@ import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
import com.couchbase.client.core.deps.com.fasterxml.jackson.databind.DeserializationFeature;
import com.couchbase.client.core.deps.com.fasterxml.jackson.databind.Module;
import com.couchbase.client.core.encryption.CryptoManager;
import com.couchbase.client.core.env.Authenticator;
import com.couchbase.client.core.env.PasswordAuthenticator;
@@ -60,7 +59,6 @@ import com.couchbase.client.java.encryption.databind.jackson.EncryptionModule;
import com.couchbase.client.java.env.ClusterEnvironment;
import com.couchbase.client.java.json.JacksonTransformers;
import com.couchbase.client.java.json.JsonValueModule;
import com.couchbase.client.java.json.RepackagedJsonValueModule;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
@@ -75,8 +73,6 @@ import com.fasterxml.jackson.databind.ObjectMapper;
@Configuration
public abstract class AbstractCouchbaseConfiguration {
@Autowired ObjectMapper couchbaseObjectMapper;
/**
* The connection string which allows the SDK to connect to the cluster.
* <p>
@@ -144,7 +140,7 @@ public abstract class AbstractCouchbaseConfiguration {
if (!nonShadowedJacksonPresent()) {
throw new CouchbaseException("non-shadowed Jackson not present");
}
builder.jsonSerializer(JacksonJsonSerializer.create(couchbaseObjectMapper));
builder.jsonSerializer(JacksonJsonSerializer.create(couchbaseObjectMapper()));
configureEnvironment(builder);
return builder.build();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors
* Copyright 2012-2021 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,7 +17,6 @@
package org.springframework.data.couchbase.core.convert;
import java.util.Collections;
import java.util.Optional;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.convert.ConversionService;
@@ -68,7 +67,8 @@ public abstract class AbstractCouchbaseConverter implements CouchbaseConverter,
}
/**
* Set the custom conversions.
* Set the custom conversions. Note that updating conversions requires a subsequent call to register them with the
* conversionService: conversions.registerConvertersIn(conversionService)
*
* @param conversions the conversions.
*/

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017-2020 the original author or authors.
* Copyright 2017-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -125,9 +125,6 @@ public class StringBasedN1qlQueryParser {
private final Collection<String> parameterNames = new HashSet<String>();
public final N1QLExpression parsedExpression;
private GenericConversionService conversionService = new DefaultConversionService();
private CustomConversions conversions = new CouchbaseCustomConversions(Collections.emptyList());
public StringBasedN1qlQueryParser(String statement, CouchbaseQueryMethod queryMethod, String bucketName,
CouchbaseConverter couchbaseConverter, String typeField, String typeValue, ParameterAccessor accessor,
SpelExpressionParser parser, QueryMethodEvaluationContextProvider evaluationContextProvider) {

View File

@@ -18,9 +18,12 @@ package org.springframework.data.couchbase.domain;
import java.lang.reflect.InvocationTargetException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.auditing.DateTimeProvider;
import org.springframework.data.convert.CustomConversions;
import org.springframework.data.couchbase.CouchbaseClientFactory;
import org.springframework.data.couchbase.SimpleCouchbaseClientFactory;
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
@@ -33,6 +36,7 @@ import org.springframework.data.couchbase.core.convert.translation.TranslationSe
import org.springframework.data.couchbase.core.mapping.CouchbaseMappingContext;
import org.springframework.data.couchbase.domain.time.AuditingDateTimeProvider;
import org.springframework.data.couchbase.repository.auditing.EnableCouchbaseAuditing;
import org.springframework.data.couchbase.repository.auditing.EnableReactiveCouchbaseAuditing;
import org.springframework.data.couchbase.repository.config.EnableCouchbaseRepositories;
import org.springframework.data.couchbase.repository.config.ReactiveRepositoryOperationsMapping;
import org.springframework.data.couchbase.repository.config.RepositoryOperationsMapping;
@@ -48,7 +52,12 @@ import com.couchbase.client.java.json.JacksonTransformers;
*/
@Configuration
@EnableCouchbaseRepositories
@EnableCouchbaseAuditing(auditorAwareRef = "auditorAwareRef", dateTimeProviderRef = "dateTimeProviderRef")
@EnableCouchbaseAuditing(auditorAwareRef = "auditorAwareRef", dateTimeProviderRef = "dateTimeProviderRef") // this
// activates
// auditing
@EnableReactiveCouchbaseAuditing(auditorAwareRef = "reactiveAuditorAwareRef",
dateTimeProviderRef = "dateTimeProviderRef") // this activates auditing
public class Config extends AbstractCouchbaseConfiguration {
String bucketname = "travel-sample";
String username = "Administrator";
@@ -104,6 +113,11 @@ public class Config extends AbstractCouchbaseConfiguration {
return new NaiveAuditorAware();
}
@Bean(name = "reactiveAuditorAwareRef")
public ReactiveNaiveAuditorAware testReactiveAuditorAware() {
return new ReactiveNaiveAuditorAware();
}
@Bean(name = "dateTimeProviderRef")
public DateTimeProvider testDateTimeProvider() {
return new AuditingDateTimeProvider();
@@ -113,12 +127,12 @@ public class Config extends AbstractCouchbaseConfiguration {
public void configureReactiveRepositoryOperationsMapping(ReactiveRepositoryOperationsMapping baseMapping) {
try {
// comment out references to 'protected' and 'mybucket' - they are only to show how multi-bucket would work
// ReactiveCouchbaseTemplate personTemplate =
// myReactiveCouchbaseTemplate(myCouchbaseClientFactory("protected"),new MappingCouchbaseConverter());
// ReactiveCouchbaseTemplate personTemplate = myReactiveCouchbaseTemplate(myCouchbaseClientFactory("protected"),
// (MappingCouchbaseConverter) (baseMapping.getDefault().getConverter()));
// baseMapping.mapEntity(Person.class, personTemplate); // Person goes in "protected" bucket
// ReactiveCouchbaseTemplate userTemplate = myReactiveCouchbaseTemplate(myCouchbaseClientFactory("mybucket"),new
// MappingCouchbaseConverter());
// baseMapping.mapEntity(User.class, userTemplate); // User goes in "mybucket"
// ReactiveCouchbaseTemplate userTemplate = myReactiveCouchbaseTemplate(myCouchbaseClientFactory("mybucket"),
// (MappingCouchbaseConverter) (baseMapping.getDefault().getConverter()));
//baseMapping.mapEntity(User.class, userTemplate); // User goes in "mybucket"
// everything else goes in getBucketName() ( which is travel-sample )
} catch (Exception e) {
throw e;
@@ -129,11 +143,12 @@ public class Config extends AbstractCouchbaseConfiguration {
public void configureRepositoryOperationsMapping(RepositoryOperationsMapping baseMapping) {
try {
// comment out references to 'protected' and 'mybucket' - they are only to show how multi-bucket would work
// CouchbaseTemplate personTemplate = myCouchbaseTemplate(myCouchbaseClientFactory("protected"),new
// MappingCouchbaseConverter());
// CouchbaseTemplate personTemplate = myCouchbaseTemplate(myCouchbaseClientFactory("protected"),
// (MappingCouchbaseConverter) (baseMapping.getDefault().getConverter()));
// baseMapping.mapEntity(Person.class, personTemplate); // Person goes in "protected" bucket
// CouchbaseTemplate userTemplate = myCouchbaseTemplate(myCouchbaseClientFactory("mybucket"),new
// MappingCouchbaseConverter());
// MappingCouchbaseConverter cvtr = (MappingCouchbaseConverter)baseMapping.getDefault().getConverter();
// CouchbaseTemplate userTemplate = myCouchbaseTemplate(myCouchbaseClientFactory("mybucket"),
// (MappingCouchbaseConverter) (baseMapping.getDefault().getConverter()));
// baseMapping.mapEntity(User.class, userTemplate); // User goes in "mybucket"
// everything else goes in getBucketName() ( which is travel-sample )
} catch (Exception e) {
@@ -176,10 +191,11 @@ public class Config extends AbstractCouchbaseConfiguration {
return converter;
}
/* This uses a CustomMappingCouchbaseConverter instead of MappingCouchbaseConverter */
@Override
@Bean(name = "couchbaseMappingConverter")
@Bean(name = "mappingCouchbaseConverter")
public MappingCouchbaseConverter mappingCouchbaseConverter(CouchbaseMappingContext couchbaseMappingContext,
CouchbaseCustomConversions couchbaseCustomConversions) {
CouchbaseCustomConversions couchbaseCustomConversions /* there is a customConversions() method bean */) {
// MappingCouchbaseConverter relies on a SimpleInformationMapper
// that has an getAliasFor(info) that just returns getType().getName().
// Our CustomMappingCouchbaseConverter uses a TypeBasedCouchbaseTypeMapper that will