You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by nf...@apache.org on 2024/03/12 08:38:32 UTC

(camel) branch camel-4.4.x updated: CAMEL-20551: camel-core - Avoid ignoring beans when using several repos (#13452)

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

nfilotto pushed a commit to branch camel-4.4.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-4.4.x by this push:
     new a95dba5a7a4 CAMEL-20551: camel-core - Avoid ignoring beans when using several repos (#13452)
a95dba5a7a4 is described below

commit a95dba5a7a41f853006db9f6eed7847bf4e1a4a5
Author: Nicolas Filotto <es...@users.noreply.github.com>
AuthorDate: Tue Mar 12 09:38:25 2024 +0100

    CAMEL-20551: camel-core - Avoid ignoring beans when using several repos (#13452)
    
    ## Motivation
    
    In case we have several bean repositories defined in the default registry and a bean is defined in one of the first bean repositories, the method findSingleByType won't return any result while we expect to retrieve a bean.
    
    ## Modifications:
    
    * Avoid ignoring the result of `findSingleByType` on the first bean repositories
---
 .../java/org/apache/camel/support/DefaultRegistryTest.java  | 13 +++++++++++++
 .../main/java/org/apache/camel/support/DefaultRegistry.java |  3 +++
 2 files changed, 16 insertions(+)

diff --git a/core/camel-core/src/test/java/org/apache/camel/support/DefaultRegistryTest.java b/core/camel-core/src/test/java/org/apache/camel/support/DefaultRegistryTest.java
index dec8b923e7c..3e1b1ac798e 100644
--- a/core/camel-core/src/test/java/org/apache/camel/support/DefaultRegistryTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/support/DefaultRegistryTest.java
@@ -276,6 +276,19 @@ public class DefaultRegistryTest {
         assertSame(context, lookup.getCamelContext());
     }
 
+    @Test
+    public void testFindSingleByTypeWithMultipleRepositories() {
+        SimpleRegistry sr = new SimpleRegistry();
+        Animal myAnimal = new Animal();
+        sr.bind("myAnimal", myAnimal);
+        registry.addBeanRepository(sr);
+
+        // Retrieve from the first bean repository
+        assertNotNull(registry.findSingleByType(Animal.class));
+        // Retrieve from the second bean repository
+        assertNotNull(registry.findSingleByType(Company.class));
+    }
+
     private static class MyBean implements CamelContextAware {
 
         private CamelContext camelContext;
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/DefaultRegistry.java b/core/camel-support/src/main/java/org/apache/camel/support/DefaultRegistry.java
index 76a9aa869f0..0c1971c9b19 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/DefaultRegistry.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/DefaultRegistry.java
@@ -365,6 +365,9 @@ public class DefaultRegistry extends ServiceSupport implements Registry, LocalBe
         if (found == null && repositories != null) {
             for (BeanRepository r : repositories) {
                 found = r.findSingleByType(type);
+                if (found != null) {
+                    break;
+                }
             }
         }