Description
Auto index creation support appears broken because the CouchbaseMappingContext
@Bean
is initialized before the CouchbaseTemplate
has had a chance to register the CouchbasePersistentEntityIndexCreator
as an ApplicationListener
.
Bean dependencies:
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseClientFactoryDependentConfiguration#couchbaseTemplate
requiresMappingCouchbaseConverter
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseDataConfiguration#couchbaseMappingConverter
requiresCouchbaseMappingContext
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseDataConfiguration#couchbaseMappingContext
How the beans are initialized:
CouchbaseMappingContext
implements InitializingBean
which calls CouchbaseMappingContext#initalize
to then iterate over the supplied (from factory method) initialEntitySet
and calls CouchbaseMappingContext#addPersistentEntity
on each one, which ends up publishing a MappingContextEvent
for each entity found.
CouchbaseTemplate
creates a CouchbasePersistentEntityIndexCreator
in its constructor, CouchbaseTemplate
also implements ApplicationContextAware
and when setApplicationContext()
is called ends up adding the CouchbasePersistentEntityIndexCreator
instance to the ApplicationContext
as a ApplicationListener
.
CouchbasePersistentEntityIndexCreator
implements ApplicationListener
and listens for MappingContextEvent
's, and handles the automatic index creation logic.
Possible solution:
I think ideally the correct solution will involve moving the CouchbasePersistentEntityIndexCreator
instance creation OUT of the CouchbaseTemplate
and into an appropriate @Configuration
class.
But I have a feeling that marking the CouchbaseMappingContext
@Bean
as @Lazy
might be the more practical solution? I haven't tested this theory so..