This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch CAMEL-17571 in repository https://gitbox.apache.org/repos/asf/camel.git
commit 5a5ef74b7beabcf67968d3f556bda5079da53b4d Author: Claus Ibsen <[email protected]> AuthorDate: Fri Mar 11 13:08:15 2022 +0100 CAMEL-17571: camel-jbang - Support for spring @Autowired/@Value annotations in custom beans --- .../camel/spi/CamelBeanPostProcessorInjector.java | 18 ++++++++++++++++-- .../impl/engine/DefaultCamelBeanPostProcessor.java | 2 +- .../org/apache/camel/main/SpringAnnotationSupport.java | 17 +++++++++++++---- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/core/camel-api/src/main/java/org/apache/camel/spi/CamelBeanPostProcessorInjector.java b/core/camel-api/src/main/java/org/apache/camel/spi/CamelBeanPostProcessorInjector.java index 51febcf..6c70ab2 100644 --- a/core/camel-api/src/main/java/org/apache/camel/spi/CamelBeanPostProcessorInjector.java +++ b/core/camel-api/src/main/java/org/apache/camel/spi/CamelBeanPostProcessorInjector.java @@ -21,12 +21,26 @@ import java.lang.reflect.Method; /** * Used for custom injection when doing {@link CamelBeanPostProcessor} bean post-processing. Can be used to support - * 3rd-party annotations for dependenct injections. + * 3rd-party annotations for dependency injections. */ public interface CamelBeanPostProcessorInjector { + /** + * Field injection + * + * @param field the field + * @param bean the bean instance where the field is present + * @param beanName optional bean id of the bean + */ void onFieldInject(Field field, Object bean, String beanName); - void onFieldMethod(Method method, Object bean, String beanName); + /** + * Method injection + * + * @param method the method + * @param bean the bean instance where the method is present + * @param beanName optional bean id of the bean + */ + void onMethodInject(Method method, Object bean, String beanName); } diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultCamelBeanPostProcessor.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultCamelBeanPostProcessor.java index 53fab39..15ad1af 100644 --- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultCamelBeanPostProcessor.java +++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultCamelBeanPostProcessor.java @@ -338,7 +338,7 @@ public class DefaultCamelBeanPostProcessor implements CamelBeanPostProcessor, Ca // custom bean injector on the method for (CamelBeanPostProcessorInjector injector : beanPostProcessorInjectors) { - injector.onFieldMethod(method, bean, beanName); + injector.onMethodInject(method, bean, beanName); } }); } diff --git a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/SpringAnnotationSupport.java b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/SpringAnnotationSupport.java index 0f0e8a9..1186ccd 100644 --- a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/SpringAnnotationSupport.java +++ b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/SpringAnnotationSupport.java @@ -21,6 +21,7 @@ import java.lang.reflect.Method; import org.apache.camel.CamelContext; import org.apache.camel.ExtendedCamelContext; +import org.apache.camel.NoSuchBeanException; import org.apache.camel.dsl.support.CompilePostProcessor; import org.apache.camel.impl.engine.CamelPostProcessorHelper; import org.apache.camel.spi.CamelBeanPostProcessor; @@ -93,8 +94,16 @@ public final class SpringAnnotationSupport { if (qualifier != null) { name = qualifier.value(); } - ReflectionHelper.setField(field, bean, - helper.getInjectionBeanValue(field.getType(), name)); + + try { + ReflectionHelper.setField(field, bean, + helper.getInjectionBeanValue(field.getType(), name)); + } catch (NoSuchBeanException e) { + if (autowired.required()) { + throw e; + } + // not required so ignore + } } Value value = field.getAnnotation(Value.class); if (value != null) { @@ -104,8 +113,8 @@ public final class SpringAnnotationSupport { } @Override - public void onFieldMethod(Method method, Object bean, String beanName) { - + public void onMethodInject(Method method, Object bean, String beanName) { + // TODO; @Bean } } }
