[
https://issues.apache.org/jira/browse/CAMEL-24100?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Claus Ibsen closed CAMEL-24100.
-------------------------------
Resolution: Fixed
Fixed via PR https://github.com/apache/camel/pull/24760
> camel-bean: class: endpoint creates a new bean instance per message despite
> default Singleton scope
> ---------------------------------------------------------------------------------------------------
>
> Key: CAMEL-24100
> URL: https://issues.apache.org/jira/browse/CAMEL-24100
> Project: Camel
> Issue Type: Bug
> Components: camel-bean
> Affects Versions: 4.21.0
> Reporter: Federico Mariani
> Assignee: Claus Ibsen
> Priority: Major
> Fix For: 4.22.0
>
> Attachments: ClassComponentSingletonScopeTest.java
>
>
> A {{class:}} endpoint with the default {{scope=Singleton}} creates a *new
> bean instance for every message*, contradicting both the documented Singleton
> scope semantics ("the bean is created or looked up only once and reused for
> the lifetime of the endpoint") and the class component docs ("works in the
> same way as the Bean component").
> *Cause*
> {{ClassComponent.createEndpoint()}} pre-sets a {{ConstantTypeBeanHolder}} on
> the endpoint. In {{BeanEndpoint.doInit()}} the singleton-caching branch
> ({{registryBean.createCacheHolder()}}) only runs when {{getBeanHolder() ==
> null}} — so for {{class:}} endpoints it is skipped entirely and the scope
> option is never honored. {{ConstantTypeBeanHolder.getBean()}} then runs on
> *every exchange*: a registry {{findSingleByType}} type-scan plus
> {{Injector.newInstance(type)}}.
> *Consequences*
> # Stateful beans silently lose state between messages.
> # Per-message reflection/instantiation + registry type-scan overhead.
> # Inverse inconsistency: when {{bean.xxx}} options are present,
> {{ClassComponent}} builds a {{ConstantBeanHolder}} (one shared instance) — so
> {{class:Foo?scope=Prototype&bean.x=1}} silently behaves as *singleton* while
> {{class:Foo?scope=Singleton}} behaves as *prototype*.
> *Suggested fix*
> In {{BeanEndpoint.doInit()}}, when the pre-set holder is a
> {{ConstantTypeBeanHolder}} and scope is Singleton, call
> {{holder.createCacheHolder()}}; or make {{ClassComponent}} scope-aware when
> choosing the holder type.
> *Reproducer*
> Attached test {{ClassComponentSingletonScopeTest}} fails on main
> (4.22.0-SNAPSHOT): 2 messages create 2 instances.
> {noformat}
> org.opentest4j.AssertionFailedError: Singleton scope should create the bean
> only once ==> expected: <1> but was: <2>
> {noformat}
> _This issue was found by an AI-assisted code review. Reported by Claude Code
> on behalf of [~fmariani] (GitHub: Croway)._
--
This message was sent by Atlassian Jira
(v8.20.10#820010)