I have a Spring Boot based library (using spring-data-mongo) that creates a PersistentEntities
bean. PersistentEntities
happens to implement the Supplier<T>
interface, so the Spring Cloud Stream functional binder is creating a binding to it. More specifically, BeanFactoryAwareFunctionRegistry.discoverDefaultDefinitionIfNecessary
finds it as a bean of type Supplier
.
We are using the Spring Cloud Streams Kafka binder, so Spring tries to publish each of these objects to a Kafka topic it creates. This causes an infinite recursion issue in the JSON serializer:
2019-12-04 15:36:54.323 ERROR 1 --- [ scheduling-1] o.s.i.h.LoggingHandler : org.springframework.messaging.MessagingException: Failed to invoke method; nested exception is org.springframework.messaging.converter.MessageConversionException: Could not write JSON: Infinite recursion (StackOverflowError) (through reference chain: org.springframework.data.mongodb.core.mapping.BasicMongoPersistentEntity["idProperty"] -> org.springframework.data.mongodb.core.mapping.CachingMongoPersistentProperty["owner"] -> org.springframework.data.mongodb.core.mapping.BasicMongoPersistentEntity["idProperty"] -> org.springframework.data.mongodb.core.mapping.CachingMongoPersistentProperty["owner"] -> org.springframework.data.mongodb.core.mapping.BasicMongoPersistentEntity["idProperty"] -> org.springframework.data.mongodb.core.mapping.CachingMongoPersistentProperty["owner"] -> org.springframework.data.mongodb.core.mapping.BasicMongoPersistentEntity["idProperty"] -> org.springframework.data.mongodb.core.mapping.CachingMongoPersistentProperty["owner"] -> org.springframework.data.mongodb.core.mapping.BasicMongoPersistentEntity["idProperty"] -> org.springframework.data.mongodb.core.mapping.CachingMongoPersistentProperty["owner"] -> org.springframework.data.mongodb.core.mapping.BasicMongoPersistentEntity["idProperty"] -> org.springframework.data.mongodb.core.mapping.CachingMongoPersistentProperty["owner"] ...
Is there a way to exclude my bean from function binding? The project consuming this library isn't using Spring Cloud Function, but I'd prefer to leave that possibility open.
For reference, my bean is defined as:
@Bean
public PersistentEntities myPersistentEntities(List<MongoTemplate> mongoTemplates) {
return new PersistentEntities(() -> {
List<MappingContext<?, ?>> mappingContexts = mongoTemplates.stream().map(t -> t.getConverter().getMappingContext()).collect(Collectors.toList());
return mappingContexts.iterator();
});
}
We just upgraded Spring Cloud from Greenwich to Hoxton, so the automatic functional bindings are new to us.