diff --git a/components/camel-spring/src/main/java/org/apache/camel/spring/spi/ApplicationContextRegistry.java b/components/camel-spring/src/main/java/org/apache/camel/spring/spi/ApplicationContextRegistry.java index 3a743b7f0d7a5..4b4d8610cb4e1 100644 --- a/components/camel-spring/src/main/java/org/apache/camel/spring/spi/ApplicationContextRegistry.java +++ b/components/camel-spring/src/main/java/org/apache/camel/spring/spi/ApplicationContextRegistry.java @@ -22,6 +22,7 @@ import org.apache.camel.NoSuchBeanException; import org.apache.camel.spi.Registry; +import org.springframework.beans.factory.BeanFactoryUtils; import org.springframework.beans.factory.BeanNotOfRequiredTypeException; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.context.ApplicationContext; @@ -75,13 +76,13 @@ public Object lookupByName(String name) { @Override public Set findByType(Class type) { - Map map = applicationContext.getBeansOfType(type); + Map map = findByTypeWithName(type); return new HashSet(map.values()); } @Override public Map findByTypeWithName(Class type) { - return applicationContext.getBeansOfType(type); + return BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, type); } @Override diff --git a/components/camel-spring/src/test/java/org/apache/camel/spring/spi/ParentContextRegistryTest.java b/components/camel-spring/src/test/java/org/apache/camel/spring/spi/ParentContextRegistryTest.java new file mode 100644 index 0000000000000..c984bfdeb23fb --- /dev/null +++ b/components/camel-spring/src/test/java/org/apache/camel/spring/spi/ParentContextRegistryTest.java @@ -0,0 +1,55 @@ +/** + * 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.spring.SpringTestSupport; +import org.junit.Assert; +import org.springframework.context.support.AbstractXmlApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import java.util.Collections; +import java.util.List; + +public class ParentContextRegistryTest extends SpringTestSupport { + private static final List expectedBean = Collections.singletonList("TestValue"); + @Override + protected AbstractXmlApplicationContext createApplicationContext() { + ClassPathXmlApplicationContext parentContext = new ClassPathXmlApplicationContext( + "parentContextRegistryTestParent.xml", ParentContextRegistryTest.class); + return new ClassPathXmlApplicationContext( + new String[] {"parentContextRegistryTestChild.xml"}, + ParentContextRegistryTest.class, parentContext + ); + } + + public void testLookupByName() { + Assert.assertEquals(expectedBean, context.getRegistry().lookupByName("testParentBean")); + } + + public void testLookupByNameAndType() { + Assert.assertEquals(expectedBean, context.getRegistry().lookupByNameAndType("testParentBean", List.class)); + } + + public void testFindByType() { + Assert.assertEquals(Collections.singleton(expectedBean), context.getRegistry().findByType(List.class)); + } + + public void testFindByTypeWithName() { + Assert.assertEquals(Collections.singletonMap("testParentBean", expectedBean), + context.getRegistry().findByTypeWithName(List.class)); + } +} diff --git a/components/camel-spring/src/test/resources/org/apache/camel/spring/spi/parentContextRegistryTestChild.xml b/components/camel-spring/src/test/resources/org/apache/camel/spring/spi/parentContextRegistryTestChild.xml new file mode 100644 index 0000000000000..4051126aecf01 --- /dev/null +++ b/components/camel-spring/src/test/resources/org/apache/camel/spring/spi/parentContextRegistryTestChild.xml @@ -0,0 +1,26 @@ + + + + + + \ No newline at end of file diff --git a/components/camel-spring/src/test/resources/org/apache/camel/spring/spi/parentContextRegistryTestParent.xml b/components/camel-spring/src/test/resources/org/apache/camel/spring/spi/parentContextRegistryTestParent.xml new file mode 100644 index 0000000000000..f8171363a78ed --- /dev/null +++ b/components/camel-spring/src/test/resources/org/apache/camel/spring/spi/parentContextRegistryTestParent.xml @@ -0,0 +1,28 @@ + + + + + TestValue + + \ No newline at end of file