diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanInfo.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanInfo.java index f51695b2796a1..904e8cfdfdbf1 100644 --- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanInfo.java +++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanInfo.java @@ -44,7 +44,6 @@ import org.apache.camel.PropertyInject; import org.apache.camel.Variable; import org.apache.camel.Variables; -import org.apache.camel.support.DefaultExchange; import org.apache.camel.support.ObjectHelper; import org.apache.camel.support.builder.ExpressionBuilder; import org.apache.camel.support.language.AnnotationExpressionFactory; @@ -163,18 +162,12 @@ public BeanInfo(CamelContext camelContext, Class type, Object instance, Metho operationsWithHandlerAnnotation = Collections.unmodifiableList(operationsWithHandlerAnnotation); methodMap = Collections.unmodifiableMap(methodMap); - // key must be instance based for custom/handler annotations - boolean instanceBased = !operationsWithCustomAnnotation.isEmpty() || !operationsWithHandlerAnnotation.isEmpty(); - // do not cache Exchange based beans - instanceBased &= DefaultExchange.class != type; - if (instanceBased) { - // add new bean info to cache (instance based) - component.addBeanInfoToCache(key, this); - } else { - // add new bean info to cache (not instance based, favor key2 if possible) - BeanInfoCacheKey k = key2 != null ? key2 : key; - component.addBeanInfoToCache(k, this); - } + // always use class-based caching (key2 when available) since introspection + // results depend on the class type, not the instance identity. + // Instance-based keys cause unbounded cache growth for ephemeral message bodies + // used with OGNL expressions like ${body.xxx} (CAMEL-23282). + BeanInfoCacheKey k = key2 != null ? key2 : key; + component.addBeanInfoToCache(k, this); } public Class getType() { diff --git a/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanInfoCacheBodyOgnlTest.java b/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanInfoCacheBodyOgnlTest.java new file mode 100644 index 0000000000000..ff5f8a427be73 --- /dev/null +++ b/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanInfoCacheBodyOgnlTest.java @@ -0,0 +1,80 @@ +/* + * 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.Exchange; +import org.apache.camel.builder.RouteBuilder; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Test for CAMEL-23282: Body OGNL expressions like ${body.name} must use class-based BeanInfo caching, not + * instance-based, even when the body class has methods with Exchange/Message parameters or handler annotations. + */ +public class BeanInfoCacheBodyOgnlTest extends ContextTestSupport { + + @Test + public void testBodyOgnlCacheUsesClassKey() throws Exception { + BeanComponent bean = context.getComponent("bean", BeanComponent.class); + + assertEquals(0, bean.getCurrentBeanCacheSize()); + + getMockEndpoint("mock:result").expectedMessageCount(5); + + for (int i = 0; i < 5; i++) { + template.sendBody("direct:start", new MyOrder("order-" + i)); + } + + assertMockEndpointsSatisfied(); + + // cache should have 1 entry (class-based), not 5 (instance-based) + assertEquals(1, bean.getCurrentBeanCacheSize()); + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from("direct:start") + .setHeader("orderName", simple("${body.name}")) + .to("mock:result"); + } + }; + } + + /** + * Body class with a method accepting Exchange — this previously triggered instance-based BeanInfo caching. + */ + public static class MyOrder { + private final String name; + + public MyOrder(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void process(Exchange exchange) { + // triggers hasCustomAnnotation=true in BeanInfo introspection + } + } +}