This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch fix/CAMEL-24067 in repository https://gitbox.apache.org/repos/asf/camel.git
commit c32a5c8aab218a5c1bbbe7984c2bf72b2787649b Author: Claus Ibsen <[email protected]> AuthorDate: Wed Jul 15 12:49:55 2026 +0200 CAMEL-24067: Fix SpringInjector not injecting CamelContext into CamelContextAware beans created via factory method Co-Authored-By: Claude Opus 4.6 <[email protected]> Signed-off-by: Claus Ibsen <[email protected]> --- .../spi/SpringInjectorFactoryMethodTest.java | 81 ++++++++++++++++++++++ .../apache/camel/spring/SpringCamelContext.java | 6 +- .../apache/camel/spring/spi/SpringInjector.java | 13 ++++ 3 files changed, 98 insertions(+), 2 deletions(-) diff --git a/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/spring/spi/SpringInjectorFactoryMethodTest.java b/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/spring/spi/SpringInjectorFactoryMethodTest.java new file mode 100644 index 000000000000..40cbd00aa635 --- /dev/null +++ b/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/spring/spi/SpringInjectorFactoryMethodTest.java @@ -0,0 +1,81 @@ +/* + * 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.spring.spi; + +import org.apache.camel.CamelContext; +import org.apache.camel.CamelContextAware; +import org.apache.camel.spring.SpringCamelContext; +import org.junit.jupiter.api.Test; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertSame; + +public class SpringInjectorFactoryMethodTest { + + @Test + void factoryMethodBeanGetsCamelContext() throws Exception { + try (AnnotationConfigApplicationContext appCtx = new AnnotationConfigApplicationContext()) { + appCtx.refresh(); + SpringCamelContext camel = new SpringCamelContext(appCtx); + camel.start(); + try { + MyBean bean = camel.getInjector().newInstance(MyBean.class, "createMyBean"); + assertNotNull(bean, "Factory method should return a non-null bean"); + assertNotNull(bean.getCamelContext(), "CamelContext should be injected into CamelContextAware bean"); + assertSame(camel, bean.getCamelContext()); + } finally { + camel.stop(); + } + } + } + + @Test + void factoryMethodWithFactoryClassGetsCamelContext() throws Exception { + try (AnnotationConfigApplicationContext appCtx = new AnnotationConfigApplicationContext()) { + appCtx.refresh(); + SpringCamelContext camel = new SpringCamelContext(appCtx); + camel.start(); + try { + MyBean bean = camel.getInjector().newInstance(MyBean.class, MyBean.class, "createMyBean"); + assertNotNull(bean, "Factory method should return a non-null bean"); + assertNotNull(bean.getCamelContext(), "CamelContext should be injected into CamelContextAware bean"); + assertSame(camel, bean.getCamelContext()); + } finally { + camel.stop(); + } + } + } + + public static class MyBean implements CamelContextAware { + private CamelContext camelContext; + + public static MyBean createMyBean() { + return new MyBean(); + } + + @Override + public CamelContext getCamelContext() { + return camelContext; + } + + @Override + public void setCamelContext(CamelContext camelContext) { + this.camelContext = camelContext; + } + } +} diff --git a/components/camel-spring-parent/camel-spring/src/main/java/org/apache/camel/spring/SpringCamelContext.java b/components/camel-spring-parent/camel-spring/src/main/java/org/apache/camel/spring/SpringCamelContext.java index 9c7726e45254..af67acd6dd7c 100644 --- a/components/camel-spring-parent/camel-spring/src/main/java/org/apache/camel/spring/SpringCamelContext.java +++ b/components/camel-spring-parent/camel-spring/src/main/java/org/apache/camel/spring/SpringCamelContext.java @@ -216,8 +216,10 @@ public class SpringCamelContext extends DefaultCamelContext @Override protected Injector createInjector() { - if (applicationContext instanceof ConfigurableApplicationContext) { - return new SpringInjector((ConfigurableApplicationContext) applicationContext); + if (applicationContext instanceof ConfigurableApplicationContext cac) { + SpringInjector answer = new SpringInjector(cac); + answer.setCamelContext(this); + return answer; } else { LOG.warn("Cannot use SpringInjector as applicationContext is not a ConfigurableApplicationContext as its: {}", applicationContext); diff --git a/components/camel-spring-parent/camel-spring/src/main/java/org/apache/camel/spring/spi/SpringInjector.java b/components/camel-spring-parent/camel-spring/src/main/java/org/apache/camel/spring/spi/SpringInjector.java index bd3c1626e700..abd9b4ecc29d 100644 --- a/components/camel-spring-parent/camel-spring/src/main/java/org/apache/camel/spring/spi/SpringInjector.java +++ b/components/camel-spring-parent/camel-spring/src/main/java/org/apache/camel/spring/spi/SpringInjector.java @@ -19,6 +19,8 @@ package org.apache.camel.spring.spi; import java.lang.reflect.Method; import java.lang.reflect.Modifier; +import org.apache.camel.CamelContext; +import org.apache.camel.CamelContextAware; import org.apache.camel.RuntimeCamelException; import org.apache.camel.spi.Injector; import org.springframework.beans.factory.config.AutowireCapableBeanFactory; @@ -29,6 +31,7 @@ import org.springframework.context.ConfigurableApplicationContext; */ public class SpringInjector implements Injector { private final ConfigurableApplicationContext applicationContext; + private CamelContext camelContext; private int autowireMode = AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR; private boolean dependencyCheck; @@ -58,6 +61,8 @@ public class SpringInjector implements Injector { Object obj = fm.invoke(null); answer = type.cast(obj); } + // inject camel context if needed + CamelContextAware.trySetCamelContext(answer, camelContext); } catch (Exception e) { throw new RuntimeCamelException("Error invoking factory method: " + factoryMethod + " on class: " + target, e); } @@ -96,6 +101,14 @@ public class SpringInjector implements Injector { this.dependencyCheck = dependencyCheck; } + public CamelContext getCamelContext() { + return camelContext; + } + + public void setCamelContext(CamelContext camelContext) { + this.camelContext = camelContext; + } + public ConfigurableApplicationContext getApplicationContext() { return applicationContext; }
