From 5f92501a0dc0171510e891560d21dbb0dea5c594 Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Tue, 15 Sep 2020 14:13:17 -0400 Subject: [PATCH] Performance improvements to NamedContextFactory. (#826) Fixes gh-825 --- .../context/named/NamedContextFactory.java | 14 ++++---- .../named/NamedContextFactoryTests.java | 36 +++++++++++++++++++ 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/context/named/NamedContextFactory.java b/spring-cloud-context/src/main/java/org/springframework/cloud/context/named/NamedContextFactory.java index 7ac1384b..54ae7d64 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/context/named/NamedContextFactory.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/context/named/NamedContextFactory.java @@ -27,6 +27,7 @@ import java.util.concurrent.ConcurrentHashMap; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactoryUtils; import org.springframework.beans.factory.DisposableBean; +import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; import org.springframework.context.ApplicationContext; @@ -143,10 +144,12 @@ public abstract class NamedContextFactory T getInstance(String name, Class type) { AnnotationConfigApplicationContext context = getContext(name); - if (BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context, - type).length > 0) { + try { return context.getBean(type); } + catch (NoSuchBeanDefinitionException e) { + // ignore + } return null; } @@ -181,11 +184,8 @@ public abstract class NamedContextFactory Map getInstances(String name, Class type) { AnnotationConfigApplicationContext context = getContext(name); - if (BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context, - type).length > 0) { - return BeanFactoryUtils.beansOfTypeIncludingAncestors(context, type); - } - return null; + + return BeanFactoryUtils.beansOfTypeIncludingAncestors(context, type); } /** diff --git a/spring-cloud-context/src/test/java/org/springframework/cloud/context/named/NamedContextFactoryTests.java b/spring-cloud-context/src/test/java/org/springframework/cloud/context/named/NamedContextFactoryTests.java index 2424683b..b5d3720b 100644 --- a/spring-cloud-context/src/test/java/org/springframework/cloud/context/named/NamedContextFactoryTests.java +++ b/spring-cloud-context/src/test/java/org/springframework/cloud/context/named/NamedContextFactoryTests.java @@ -54,6 +54,18 @@ public class NamedContextFactoryTests { Bar foobar = factory.getInstance("foo", Bar.class); then(foobar).as("bar was not null").isNull(); + Baz fooBaz = factory.getInstance("foo", Baz.class); + then(fooBaz).as("fooBaz was null").isNotNull(); + + Object fooContainerFoo = factory.getInstance("foo", Container.class, Foo.class); + then(fooContainerFoo).as("fooContainerFoo was null").isNotNull(); + + Object fooContainerBar = factory.getInstance("foo", Container.class, Bar.class); + then(fooContainerBar).as("fooContainerBar was not null").isNull(); + + Object barContainerBar = factory.getInstance("bar", Container.class, Bar.class); + then(barContainerBar).as("barContainerBar was null").isNotNull(); + Map fooBazes = factory.getInstances("foo", Baz.class); then(fooBazes).as("fooBazes was null").isNotNull(); then(fooBazes.size()).as("fooBazes size was wrong").isEqualTo(1); @@ -146,6 +158,11 @@ public class NamedContextFactoryTests { return new Foo(); } + @Bean + Container fooContainer() { + return new Container<>(new Foo()); + } + } static class Foo { @@ -164,10 +181,29 @@ public class NamedContextFactoryTests { return new Baz(); } + @Bean + Container barContainer() { + return new Container<>(new Bar()); + } + } static class Bar { } + static class Container { + + private final T item; + + Container(T item) { + this.item = item; + } + + public T getItem() { + return this.item; + } + + } + }