This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch fix/CAMEL-24100 in repository https://gitbox.apache.org/repos/asf/camel.git
commit 3ee1cb330911804424655e3bad8903d0ed6661db Author: Claus Ibsen <[email protected]> AuthorDate: Thu Jul 16 09:38:04 2026 +0200 CAMEL-24100: camel-bean - class: endpoint honors scope option Co-Authored-By: Claude Opus 4.6 <[email protected]> Signed-off-by: Claus Ibsen <[email protected]> --- .../apache/camel/component/bean/BeanEndpoint.java | 11 +++ .../camel/component/beanclass/ClassComponent.java | 5 +- .../component/bean/ClassComponentScopeTest.java | 106 +++++++++++++++++++++ .../camel/component/bean/MyCountingBean.java | 49 ++++++++++ 4 files changed, 170 insertions(+), 1 deletion(-) diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanEndpoint.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanEndpoint.java index 0d01f93417d0..5817138bf2f9 100644 --- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanEndpoint.java +++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanEndpoint.java @@ -112,6 +112,17 @@ public class BeanEndpoint extends DefaultEndpoint { } else { holder = registryBean; } + } else if (holder instanceof ConstantTypeBeanHolder typeBeanHolder && scope == BeanScope.Singleton) { + // ClassComponent pre-sets a ConstantTypeBeanHolder which creates a new instance per exchange, + // so for singleton scope we must cache the bean instance + holder = typeBeanHolder.createCacheHolder(); + } else if (holder instanceof ConstantBeanHolder && scope == BeanScope.Prototype) { + // ClassComponent pre-sets a ConstantBeanHolder (single instance) when bean.xxx options are present, + // so for prototype scope we must use a type holder that creates new instances per exchange + ParameterMappingStrategy strategy + = ParameterMappingStrategyHelper.createParameterMappingStrategy(getCamelContext()); + BeanComponent bean = getCamelContext().getComponent("bean", BeanComponent.class); + holder = new ConstantTypeBeanHolder(holder.getBeanInfo().getType(), getCamelContext(), strategy, bean); } if (scope == BeanScope.Request) { // wrap in registry scoped diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/beanclass/ClassComponent.java b/components/camel-bean/src/main/java/org/apache/camel/component/beanclass/ClassComponent.java index dd75171c4da7..ce9a996dc875 100644 --- a/components/camel-bean/src/main/java/org/apache/camel/component/beanclass/ClassComponent.java +++ b/components/camel-bean/src/main/java/org/apache/camel/component/beanclass/ClassComponent.java @@ -16,6 +16,7 @@ */ package org.apache.camel.component.beanclass; +import java.util.LinkedHashMap; import java.util.Map; import org.apache.camel.Endpoint; @@ -54,7 +55,9 @@ public class ClassComponent extends BeanComponent { // the bean.xxx options is for the bean Map<String, Object> options = PropertiesHelper.extractProperties(parameters, "bean."); - endpoint.setParameters(options); + // store a copy because setProperties below removes bound entries from the original map, + // but BeanEndpoint needs the options to apply them per-exchange for prototype scope + endpoint.setParameters(options.isEmpty() ? options : new LinkedHashMap<>(options)); BeanHolder holder; diff --git a/core/camel-core/src/test/java/org/apache/camel/component/bean/ClassComponentScopeTest.java b/core/camel-core/src/test/java/org/apache/camel/component/bean/ClassComponentScopeTest.java new file mode 100644 index 000000000000..0d32b919f7d8 --- /dev/null +++ b/core/camel-core/src/test/java/org/apache/camel/component/bean/ClassComponentScopeTest.java @@ -0,0 +1,106 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.bean; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.RouteBuilder; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ClassComponentScopeTest extends ContextTestSupport { + + @Test + public void testDefaultSingletonScope() throws Exception { + getMockEndpoint("mock:result").expectedBodiesReceived("Hello A", "Hello B"); + + int before = MyCountingBean.getInstanceCount(); + template.sendBody("direct:singleton", "A"); + template.sendBody("direct:singleton", "B"); + + assertMockEndpointsSatisfied(); + + assertEquals(0, MyCountingBean.getInstanceCount() - before, + "Singleton scope should not create new bean instances per exchange"); + } + + @Test + public void testPrototypeScope() throws Exception { + getMockEndpoint("mock:result").expectedBodiesReceived("Hello A", "Hello B"); + + int before = MyCountingBean.getInstanceCount(); + template.sendBody("direct:prototype", "A"); + template.sendBody("direct:prototype", "B"); + + assertMockEndpointsSatisfied(); + + assertEquals(2, MyCountingBean.getInstanceCount() - before, + "Prototype scope should create a new bean per exchange"); + } + + @Test + public void testSingletonScopeWithBeanOptions() throws Exception { + getMockEndpoint("mock:result").expectedBodiesReceived("Bye A", "Bye B"); + + int before = MyCountingBean.getInstanceCount(); + template.sendBody("direct:singletonWithOptions", "A"); + template.sendBody("direct:singletonWithOptions", "B"); + + assertMockEndpointsSatisfied(); + + assertEquals(0, MyCountingBean.getInstanceCount() - before, + "Singleton scope with bean options should not create new bean instances per exchange"); + } + + @Test + public void testPrototypeScopeWithBeanOptions() throws Exception { + getMockEndpoint("mock:result").expectedBodiesReceived("Bye A", "Bye B"); + + int before = MyCountingBean.getInstanceCount(); + template.sendBody("direct:prototypeWithOptions", "A"); + template.sendBody("direct:prototypeWithOptions", "B"); + + assertMockEndpointsSatisfied(); + + assertEquals(2, MyCountingBean.getInstanceCount() - before, + "Prototype scope with bean options should create a new bean per exchange"); + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from("direct:singleton") + .to("class:org.apache.camel.component.bean.MyCountingBean") + .to("mock:result"); + + from("direct:prototype") + .to("class:org.apache.camel.component.bean.MyCountingBean?scope=Prototype") + .to("mock:result"); + + from("direct:singletonWithOptions") + .to("class:org.apache.camel.component.bean.MyCountingBean?bean.prefix=Bye") + .to("mock:result"); + + from("direct:prototypeWithOptions") + .to("class:org.apache.camel.component.bean.MyCountingBean?scope=Prototype&bean.prefix=Bye") + .to("mock:result"); + } + }; + } +} diff --git a/core/camel-core/src/test/java/org/apache/camel/component/bean/MyCountingBean.java b/core/camel-core/src/test/java/org/apache/camel/component/bean/MyCountingBean.java new file mode 100644 index 000000000000..b0ceda29de2a --- /dev/null +++ b/core/camel-core/src/test/java/org/apache/camel/component/bean/MyCountingBean.java @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.bean; + +import java.util.concurrent.atomic.AtomicInteger; + +public class MyCountingBean { + private static final AtomicInteger INSTANCES = new AtomicInteger(); + + private String prefix = "Hello"; + + public MyCountingBean() { + INSTANCES.incrementAndGet(); + } + + public static int getInstanceCount() { + return INSTANCES.get(); + } + + public static void resetInstanceCount() { + INSTANCES.set(0); + } + + public String getPrefix() { + return prefix; + } + + public void setPrefix(String prefix) { + this.prefix = prefix; + } + + public String hello(String s) { + return prefix + " " + s; + } +}
