[
https://issues.apache.org/jira/browse/CAMEL-24305?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18100290#comment-18100290
]
Guillaume Nodet commented on CAMEL-24305:
-----------------------------------------
This issue is being investigated by a coding agent (Claude Code on behalf of
gnodet).
Analysis: Lifecycle race confirmed. KafkaComponent.doInit() creates
DefaultKafkaClientFactory (line 306) before
DefaultAutowiredLifecycleStrategy.onComponentAdd() runs in the addComponent()
path used by Spring Boot. By the time autowiring checks kafkaClientFactory, it
is already non-null, so custom beans are skipped. This is a regression from
CAMEL-17262 (commit 74c5a4ad2a7) which reintroduced the bug originally fixed in
CAMEL-16500. Fix: move default factory creation from doInit() to doStart().
_Note: This comment was generated by a coding agent and requires manual
verification._
> 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)