3939import org .springframework .data .couchbase .core .ReactiveCouchbaseTemplate ;
4040import org .springframework .data .couchbase .core .convert .CouchbaseCustomConversions ;
4141import org .springframework .data .couchbase .core .convert .CouchbasePropertyValueConverterFactory ;
42- import org .springframework .data .couchbase .core .convert .CryptoConverter ;
4342import org .springframework .data .couchbase .core .convert .MappingCouchbaseConverter ;
4443import org .springframework .data .couchbase .core .convert .translation .JacksonTranslationService ;
4544import org .springframework .data .couchbase .core .convert .translation .TranslationService ;
8887@ Configuration
8988public abstract class AbstractCouchbaseConfiguration {
9089
90+ ObjectMapper mapper ;
91+ CryptoManager cryptoManager =null ;
92+
9193 /**
9294 * The connection string which allows the SDK to connect to the cluster.
9395 * <p>
@@ -155,9 +157,8 @@ public ClusterEnvironment couchbaseClusterEnvironment() {
155157 if (!nonShadowedJacksonPresent ()) {
156158 throw new CouchbaseException ("non-shadowed Jackson not present" );
157159 }
158- CryptoManager cryptoManager = cryptoManager ();
159- builder .jsonSerializer (JacksonJsonSerializer .create (couchbaseObjectMapper (cryptoManager )));
160- builder .cryptoManager (cryptoManager );
160+ builder .jsonSerializer (JacksonJsonSerializer .create (getCouchbaseObjectMapper ()));
161+ builder .cryptoManager (getCryptoManager ());
161162 configureEnvironment (builder );
162163 return builder .build ();
163164 }
@@ -177,24 +178,13 @@ public CouchbaseTemplate couchbaseTemplate(CouchbaseClientFactory couchbaseClien
177178 getDefaultConsistency ());
178179 }
179180
180- public CouchbaseTemplate couchbaseTemplate (CouchbaseClientFactory couchbaseClientFactory ,
181- MappingCouchbaseConverter mappingCouchbaseConverter ) {
182- return couchbaseTemplate (couchbaseClientFactory , mappingCouchbaseConverter , new JacksonTranslationService ());
183- }
184-
185181 @ Bean (name = BeanNames .REACTIVE_COUCHBASE_TEMPLATE )
186182 public ReactiveCouchbaseTemplate reactiveCouchbaseTemplate (CouchbaseClientFactory couchbaseClientFactory ,
187183 MappingCouchbaseConverter mappingCouchbaseConverter , TranslationService couchbaseTranslationService ) {
188184 return new ReactiveCouchbaseTemplate (couchbaseClientFactory , mappingCouchbaseConverter , couchbaseTranslationService ,
189185 getDefaultConsistency ());
190186 }
191187
192- public ReactiveCouchbaseTemplate reactiveCouchbaseTemplate (CouchbaseClientFactory couchbaseClientFactory ,
193- MappingCouchbaseConverter mappingCouchbaseConverter ) {
194- return reactiveCouchbaseTemplate (couchbaseClientFactory , mappingCouchbaseConverter ,
195- new JacksonTranslationService ());
196- }
197-
198188 @ Bean (name = BeanNames .COUCHBASE_OPERATIONS_MAPPING )
199189 public RepositoryOperationsMapping couchbaseRepositoryOperationsMapping (CouchbaseTemplate couchbaseTemplate ) {
200190 // create a base mapping that associates all repositories to the default template
@@ -288,7 +278,7 @@ public MappingCouchbaseConverter mappingCouchbaseConverter(CouchbaseMappingConte
288278 @ Bean
289279 public TranslationService couchbaseTranslationService () {
290280 final JacksonTranslationService jacksonTranslationService = new JacksonTranslationService ();
291- jacksonTranslationService .setObjectMapper (couchbaseObjectMapper ( cryptoManager () ));
281+ jacksonTranslationService .setObjectMapper (getCouchbaseObjectMapper ( ));
292282 jacksonTranslationService .afterPropertiesSet ();
293283 // for sdk3, we need to ask the mapper _it_ uses to ignore extra fields...
294284 JacksonTransformers .MAPPER .configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false );
@@ -309,35 +299,27 @@ public CouchbaseMappingContext couchbaseMappingContext(CustomConversions customC
309299 return mappingContext ;
310300 }
311301
312- /**
313- * Creates a {@link ObjectMapper} for the jsonSerializer of the ClusterEnvironment
314- *
315- * @return ObjectMapper
316- */
317- private ObjectMapper couchbaseObjectMapper () {
318- return couchbaseObjectMapper (cryptoManager ());
302+
303+ private ObjectMapper getCouchbaseObjectMapper () {
304+ if (mapper != null ) {
305+ return mapper ;
306+ }
307+ return mapper = couchbaseObjectMapper ();
319308 }
320309
321310 /**
322311 * Creates a {@link ObjectMapper} for the jsonSerializer of the ClusterEnvironment
323312 *
324- * @param cryptoManager
325313 * @return ObjectMapper
326314 */
327-
328- ObjectMapper mapper ;
329-
330- public ObjectMapper couchbaseObjectMapper (CryptoManager cryptoManager ) {
331- if (mapper != null ) {
332- return mapper ;
333- }
334- mapper = new ObjectMapper (); // or use the one from the Java SDK (?) JacksonTransformers.MAPPER
335- mapper .configure (com .fasterxml .jackson .databind .DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false );
336- mapper .registerModule (new JsonValueModule ());
337- if (cryptoManager != null ) {
338- mapper .registerModule (new EncryptionModule (cryptoManager ));
315+ public ObjectMapper couchbaseObjectMapper () {
316+ ObjectMapper om = new ObjectMapper (); // or use the one from the Java SDK (?) JacksonTransformers.MAPPER
317+ om .configure (com .fasterxml .jackson .databind .DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false );
318+ om .registerModule (new JsonValueModule ());
319+ if (getCryptoManager () != null ) {
320+ om .registerModule (new EncryptionModule (getCryptoManager ()));
339321 }
340- return mapper ;
322+ return om ;
341323 }
342324
343325 /**
@@ -405,7 +387,7 @@ protected boolean autoIndexCreation() {
405387 */
406388 @ Bean (name = BeanNames .COUCHBASE_CUSTOM_CONVERSIONS )
407389 public CustomConversions customConversions () {
408- return customConversions (cryptoManager ());
390+ return customConversions (getCryptoManager ());
409391 }
410392
411393 /**
@@ -428,14 +410,18 @@ public CustomConversions customConversions(CryptoManager cryptoManager) {
428410 return customConversions ;
429411 }
430412
431- @ Bean
432- protected CryptoManager cryptoManager () {
433- return null ;
413+ /**
414+ * cryptoManager can be null, so it cannot be a bean and then used as an arg for bean methods
415+ */
416+ private CryptoManager getCryptoManager () {
417+ if (cryptoManager == null ){
418+ cryptoManager = cryptoManager ();
419+ }
420+ return cryptoManager ;
434421 }
435422
436- @ Bean
437- protected CryptoConverter cryptoConverter (CryptoManager cryptoManager ) {
438- return cryptoManager == null ? null : new CryptoConverter (cryptoManager );
423+ protected CryptoManager cryptoManager () {
424+ return null ;
439425 }
440426
441427 /**
0 commit comments