This is an automated email from the ASF dual-hosted git repository. jamesnetherton pushed a commit to branch 3.27.x in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git
commit ecdc51e817c908bfde7067cc0a8f2beec1f4f132 Author: James Netherton <[email protected]> AuthorDate: Wed Dec 10 14:17:14 2025 +0000 Enable interaction with CDI injected beans in non RouteBuilder @BindToRegistry methods --- .../java/org/apache/camel/quarkus/core/CamelRecorder.java | 14 +++++++++++++- ...oRegistryOnCdiBean.java => BindToRegistryEchoBean.java} | 8 ++++---- .../component/bean/bind/BindToRegistryOnCdiBean.java | 12 ++++++++++++ .../camel/quarkus/component/bean/BindToRegistryTest.java | 8 ++++++++ 4 files changed, 37 insertions(+), 5 deletions(-) diff --git a/extensions-core/core/runtime/src/main/java/org/apache/camel/quarkus/core/CamelRecorder.java b/extensions-core/core/runtime/src/main/java/org/apache/camel/quarkus/core/CamelRecorder.java index da994ce9e5..f9e049e5a2 100644 --- a/extensions-core/core/runtime/src/main/java/org/apache/camel/quarkus/core/CamelRecorder.java +++ b/extensions-core/core/runtime/src/main/java/org/apache/camel/quarkus/core/CamelRecorder.java @@ -21,6 +21,7 @@ import java.util.Set; import java.util.function.Supplier; import io.quarkus.arc.Arc; +import io.quarkus.arc.InjectableInstance; import io.quarkus.runtime.RuntimeValue; import io.quarkus.runtime.annotations.Recorder; import org.apache.camel.CamelContext; @@ -220,8 +221,19 @@ public class CamelRecorder { public void postProcessBeanAndBindToRegistry(RuntimeValue<CamelContext> camelContextRuntimeValue, Class<?> beanType) { try { + Object bean; + + // To enable features like CDI injection to work with @BindToRegistry factory methods + // try to find an existing bean for the @BindToRegistry host class and use it for postprocessing + InjectableInstance<?> beanInstance = Arc.container().select(beanType); + if (beanInstance.isResolvable()) { + bean = beanInstance.get(); + } else { + // No existing CDI bean so fallback to direct instantiation + bean = beanType.getDeclaredConstructor().newInstance(); + } + CamelContext camelContext = camelContextRuntimeValue.getValue(); - Object bean = beanType.getDeclaredConstructor().newInstance(); CamelBeanPostProcessor beanPostProcessor = PluginHelper.getBeanPostProcessor(camelContext); beanPostProcessor.postProcessBeforeInitialization(bean, bean.getClass().getName()); beanPostProcessor.postProcessAfterInitialization(bean, bean.getClass().getName()); diff --git a/integration-test-groups/foundation/bean/src/main/java/org/apache/camel/quarkus/component/bean/bind/BindToRegistryOnCdiBean.java b/integration-test-groups/foundation/bean/src/main/java/org/apache/camel/quarkus/component/bean/bind/BindToRegistryEchoBean.java similarity index 84% copy from integration-test-groups/foundation/bean/src/main/java/org/apache/camel/quarkus/component/bean/bind/BindToRegistryOnCdiBean.java copy to integration-test-groups/foundation/bean/src/main/java/org/apache/camel/quarkus/component/bean/bind/BindToRegistryEchoBean.java index c5fb502cc2..ace5938227 100644 --- a/integration-test-groups/foundation/bean/src/main/java/org/apache/camel/quarkus/component/bean/bind/BindToRegistryOnCdiBean.java +++ b/integration-test-groups/foundation/bean/src/main/java/org/apache/camel/quarkus/component/bean/bind/BindToRegistryEchoBean.java @@ -17,10 +17,10 @@ package org.apache.camel.quarkus.component.bean.bind; import jakarta.enterprise.context.ApplicationScoped; -import org.apache.camel.BindToRegistry; @ApplicationScoped -public class BindToRegistryOnCdiBean { - @BindToRegistry - BindToRegistryBean bindToRegistryBeanCdiTest = new BindToRegistryBean(); +public class BindToRegistryEchoBean { + public String echo(String message) { + return message; + } } diff --git a/integration-test-groups/foundation/bean/src/main/java/org/apache/camel/quarkus/component/bean/bind/BindToRegistryOnCdiBean.java b/integration-test-groups/foundation/bean/src/main/java/org/apache/camel/quarkus/component/bean/bind/BindToRegistryOnCdiBean.java index c5fb502cc2..2da5385733 100644 --- a/integration-test-groups/foundation/bean/src/main/java/org/apache/camel/quarkus/component/bean/bind/BindToRegistryOnCdiBean.java +++ b/integration-test-groups/foundation/bean/src/main/java/org/apache/camel/quarkus/component/bean/bind/BindToRegistryOnCdiBean.java @@ -16,11 +16,23 @@ */ package org.apache.camel.quarkus.component.bean.bind; +import io.quarkus.arc.Unremovable; import jakarta.enterprise.context.ApplicationScoped; +import jakarta.inject.Inject; import org.apache.camel.BindToRegistry; +@Unremovable @ApplicationScoped public class BindToRegistryOnCdiBean { + @Inject + BindToRegistryEchoBean echoBean; + @BindToRegistry BindToRegistryBean bindToRegistryBeanCdiTest = new BindToRegistryBean(); + + @BindToRegistry + public BindToRegistryBean nonRouteBuilderBeanWithInjection() { + String message = echoBean != null ? echoBean.echo("BindToRegistryEchoBean") : "Unknown"; + return new BindToRegistryBean(message); + } } diff --git a/integration-test-groups/foundation/bean/src/test/java/org/apache/camel/quarkus/component/bean/BindToRegistryTest.java b/integration-test-groups/foundation/bean/src/test/java/org/apache/camel/quarkus/component/bean/BindToRegistryTest.java index af12ca7a76..ab383f2b70 100644 --- a/integration-test-groups/foundation/bean/src/test/java/org/apache/camel/quarkus/component/bean/BindToRegistryTest.java +++ b/integration-test-groups/foundation/bean/src/test/java/org/apache/camel/quarkus/component/bean/BindToRegistryTest.java @@ -60,4 +60,12 @@ class BindToRegistryTest { .then() .body(equalTo("BindToRegistryProcessor instantiation count: 1")); } + + @Test + void bindToRegistryNonRouteBuilderBeanWithInjection() { + RestAssured.given() + .get("/bean/route/invokeBindToRegistryBean/nonRouteBuilderBeanWithInjection") + .then() + .body(equalTo("Hello BindToRegistryEchoBean")); + } }
