spring-cloud/spring-functions-catalog

NoSuchBeanDefinitionException Consumer<Message<?>> sftpConsumer

Closed this issue · 4 comments

When using the sftp-consumer module as a dependency in a custom application, a bean of type Consumer<Message<?>> sftpConsumer is not available. However, the IntegrationFlow ftpOutboundFlow is available for injection.

Spring Boot: 3.1.5
Stream Applications 4.0.0
JDK 17

Reproducer in https://github.com/cachescrubber/sftp-consumer-demo/tree/main

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.util.function.Consumer<org.springframework.messaging.Message<?>>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier("sftpConsumer")}
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1824)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1383)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1337)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:764)

Hi @cachescrubber ,
It looks like referencing it via @Autowired as a field is too soon in the lifecycle. If you add @Lazy to the field it should work.

@Autowired
@Lazy
@Qualifier("sftpConsumer")
private Consumer<Message<?>> sftpConsumer;

We will update the docs to clarify this point as well.

Hi @onobc,

thanks for the explanation. Using @Lazy on the injection point does work. Sometimes you can't see the wood for the trees :-).

Reading the Javadoc of @Lazy I'm wondering why we do not annotate the bean definition (SftpConsumerConfiguration#ftpOutboundFlow) or the configuration class SftpConsumerConfiguration itself?

Ok, forget my last comment. just gave It a second thought. @Lazy needs to be on the injection point in this case.

I have another idea how to avoid this problem, but it will be fixed already here in this project.
Stay tuned.