Description
MappingContextEvent
contains source
field. Consumers of this event use source
field to understand which MappingContext
created the event.
The standard approach in spring-data module to use the source
would be:
class MyConsumer implements ApplicationListener<MappingContextEvent<?, ?>> {
private final MyMappingContext expectedMappingContext;
@Override
public void onApplicationEvent(MappingContextEvent<?, ?> event) {
if(event.wasEmittedBy(expectedMappingContext)) {
// process this event
}
}
Such API creates circular dependency.
Before Spring Boot 2.6 (probably changes regarding circular references were introduced in Spring to be exact, unfortunately I'm not aware which version) such circular dependencies worked, now it is disabled out of the box, is considered harmful and requires spring.main.allow-circular-references=true
property.
This API is used in spring-data-couchbase
, spring-data-mongodb
and spring-data-aerospike
for index creating. I've researched these implementations and such API makes developers create a total mess in code for dealing with circular dependency issue.
Useful links to understand how this is currently implemented and which issues are there:
spring-projects/spring-data-mongodb#2823
spring-projects/spring-data-couchbase#858
spring-projects/spring-data-couchbase#298
spring-projects/spring-data-couchbase@03ad369
https://github.com/spring-projects/spring-data-couchbase/blob/main/src/main/java/org/springframework/data/couchbase/core/CouchbaseTemplate.java#L182-L207
https://github.com/spring-projects/spring-data-mongodb/blob/main/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java#L232-L246
I've just faced the same issue in spring-data-aerospike
with Spring Boot 2.6 and found that resolving this is not that straight forward, and each spring-data implementation has it's own workarounds. So I thought that maybe there should be some other approach to resolve such issue?