[ 
https://issues.apache.org/jira/browse/CAMEL-24305?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Work on CAMEL-24305 started by Guillaume Nodet.
-----------------------------------------------
> KafkaComponent does not autowire custom KafkaClientFactory because default 
> instance is created before autowiring
> ----------------------------------------------------------------------------------------------------------------
>
>                 Key: CAMEL-24305
>                 URL: https://issues.apache.org/jira/browse/CAMEL-24305
>             Project: Camel
>          Issue Type: Bug
>          Components: camel-kafka, camel-spring-boot
>    Affects Versions: 4.21.0
>         Environment: Apache Camel 4.18.x (observed in Red Hat build 
> 4.18.1.redhat-00019)
> Spring Boot
> camel-kafka-starter
>            Reporter:  Ramu kakarla
>            Assignee: Guillaume Nodet
>            Priority: Major
>              Labels: patch
>
> The Kafka component documentation marks the kafkaClientFactory component 
> option as autowired. However, when a custom KafkaClientFactory is registered 
> as a Spring bean, it is not injected into the KafkaComponent.
> Instead, the component creates a default KafkaClientFactory during 
> initialization before the autowiring phase executes. Since the field is 
> already non-null, Camel's autowiring logic skips it.
> Expected behavior
> When a single KafkaClientFactory bean is available in the registry, it should 
> be autowired into KafkaComponent as documented.
> Actual behavior
> KafkaComponent.doInit() creates a default KafkaClientFactory before 
> LifecycleStrategySupport.doAutoWire() runs.
> The execution order is:
> KafkaComponent is instantiated.
> ComponentCustomizers execute.
> KafkaComponent.doInit() creates the default KafkaClientFactory.
> LifecycleStrategySupport.doAutoWire() executes.
> doAutoWire() skips kafkaClientFactory because the field is already populated.
> As a result, the custom KafkaClientFactory bean is never injected.
> Steps to reproduce
> Create a Spring Boot application using camel-kafka-starter.
> Define a custom implementation of KafkaClientFactory as a Spring bean.
> Use a Kafka endpoint so that KafkaComponent is initialized.
> Observe that the custom bean is not injected into KafkaComponent.
> Workaround
> Register a ComponentCustomizer that sets the KafkaClientFactory before 
> KafkaComponent.doInit() executes. This avoids creation of the default 
> instance and allows the custom implementation to be used.
> A minimal workaround implementation is attached.
> Additional information
> The attached stack trace shows the initialization sequence demonstrating that 
> KafkaComponent.doInit() executes before LifecycleStrategySupport.doAutoWire().



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to