This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new c19b9871b1bb CAMEL-24067: Fix SpringInjector not injecting
CamelContext via factory method
c19b9871b1bb is described below
commit c19b9871b1bb2b42664794de6a494577e2fc1428
Author: Claus Ibsen <[email protected]>
AuthorDate: Wed Jul 15 15:21:00 2026 +0200
CAMEL-24067: Fix SpringInjector not injecting CamelContext via factory
method
SpringInjector.newInstance(type, factoryClass, factoryMethod) was missing
the CamelContextAware.trySetCamelContext() call that DefaultInjector has,
so CamelContextAware beans created through #class:...#factoryMethod syntax
did not get the CamelContext injected in Spring/Spring Boot.
Closes #24714
Co-Authored-By: Claude Opus 4.6 <[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;
}