You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2023/10/26 10:28:32 UTC

[camel] 01/04: CAMEL-20042: camel-spring - findSingleByType should favour @Primary beans from Spring

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch primary-spring
in repository https://gitbox.apache.org/repos/asf/camel.git

commit b53e4c45abea844c5e18c3d4de9b8eb5818f8fcd
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Oct 26 09:57:14 2023 +0200

    CAMEL-20042: camel-spring - findSingleByType should favour @Primary beans from Spring
---
 .../spi/ApplicationContextBeanRepository.java      | 24 ++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/components/camel-spring/src/main/java/org/apache/camel/spring/spi/ApplicationContextBeanRepository.java b/components/camel-spring/src/main/java/org/apache/camel/spring/spi/ApplicationContextBeanRepository.java
index 95be7e1fcd9..f659bbcecfc 100644
--- a/components/camel-spring/src/main/java/org/apache/camel/spring/spi/ApplicationContextBeanRepository.java
+++ b/components/camel-spring/src/main/java/org/apache/camel/spring/spi/ApplicationContextBeanRepository.java
@@ -21,10 +21,13 @@ import java.util.Map;
 import java.util.Set;
 
 import org.apache.camel.NoSuchBeanException;
+import org.apache.camel.NoSuchBeanTypeException;
 import org.apache.camel.spi.BeanRepository;
 import org.springframework.beans.factory.BeanFactoryUtils;
 import org.springframework.beans.factory.BeanNotOfRequiredTypeException;
 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
+import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
+import org.springframework.beans.factory.config.NamedBeanHolder;
 import org.springframework.context.ApplicationContext;
 
 /**
@@ -83,4 +86,25 @@ public class ApplicationContextBeanRepository implements BeanRepository {
         return BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, type);
     }
 
+    @Override
+    public <T> T findSingleByType(Class<T> type) {
+        try {
+            NamedBeanHolder<T> holder = applicationContext.getAutowireCapableBeanFactory().resolveNamedBean(type);
+            return holder.getBeanInstance();
+        } catch (NoSuchBeanDefinitionException e) {
+            return null;
+        }
+    }
+
+    @Override
+    public <T> T mandatoryFindSingleByType(Class<T> type) {
+        try {
+            NamedBeanHolder<T> holder = applicationContext.getAutowireCapableBeanFactory().resolveNamedBean(type);
+            return holder.getBeanInstance();
+        } catch (NoUniqueBeanDefinitionException e) {
+            throw new NoSuchBeanTypeException(type, e.getNumberOfBeansFound());
+        } catch (NoSuchBeanDefinitionException e) {
+            throw new NoSuchBeanTypeException(type);
+        }
+    }
 }