Skip to content

Commit 5f92501

Browse files
authored
Performance improvements to NamedContextFactory. (#826)
Fixes gh-825
1 parent ec264fb commit 5f92501

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

spring-cloud-context/src/main/java/org/springframework/cloud/context/named/NamedContextFactory.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.beans.BeansException;
2828
import org.springframework.beans.factory.BeanFactoryUtils;
2929
import org.springframework.beans.factory.DisposableBean;
30+
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
3031
import org.springframework.beans.factory.ObjectProvider;
3132
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
3233
import org.springframework.context.ApplicationContext;
@@ -143,10 +144,12 @@ protected String generateDisplayName(String name) {
143144

144145
public <T> T getInstance(String name, Class<T> type) {
145146
AnnotationConfigApplicationContext context = getContext(name);
146-
if (BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context,
147-
type).length > 0) {
147+
try {
148148
return context.getBean(type);
149149
}
150+
catch (NoSuchBeanDefinitionException e) {
151+
// ignore
152+
}
150153
return null;
151154
}
152155

@@ -181,11 +184,8 @@ public <T> T getInstance(String name, ResolvableType type) {
181184

182185
public <T> Map<String, T> getInstances(String name, Class<T> type) {
183186
AnnotationConfigApplicationContext context = getContext(name);
184-
if (BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context,
185-
type).length > 0) {
186-
return BeanFactoryUtils.beansOfTypeIncludingAncestors(context, type);
187-
}
188-
return null;
187+
188+
return BeanFactoryUtils.beansOfTypeIncludingAncestors(context, type);
189189
}
190190

191191
/**

spring-cloud-context/src/test/java/org/springframework/cloud/context/named/NamedContextFactoryTests.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ public void testChildContexts() {
5454
Bar foobar = factory.getInstance("foo", Bar.class);
5555
then(foobar).as("bar was not null").isNull();
5656

57+
Baz fooBaz = factory.getInstance("foo", Baz.class);
58+
then(fooBaz).as("fooBaz was null").isNotNull();
59+
60+
Object fooContainerFoo = factory.getInstance("foo", Container.class, Foo.class);
61+
then(fooContainerFoo).as("fooContainerFoo was null").isNotNull();
62+
63+
Object fooContainerBar = factory.getInstance("foo", Container.class, Bar.class);
64+
then(fooContainerBar).as("fooContainerBar was not null").isNull();
65+
66+
Object barContainerBar = factory.getInstance("bar", Container.class, Bar.class);
67+
then(barContainerBar).as("barContainerBar was null").isNotNull();
68+
5769
Map<String, Baz> fooBazes = factory.getInstances("foo", Baz.class);
5870
then(fooBazes).as("fooBazes was null").isNotNull();
5971
then(fooBazes.size()).as("fooBazes size was wrong").isEqualTo(1);
@@ -146,6 +158,11 @@ Foo foo() {
146158
return new Foo();
147159
}
148160

161+
@Bean
162+
Container<Foo> fooContainer() {
163+
return new Container<>(new Foo());
164+
}
165+
149166
}
150167

151168
static class Foo {
@@ -164,10 +181,29 @@ Baz baz2() {
164181
return new Baz();
165182
}
166183

184+
@Bean
185+
Container<Bar> barContainer() {
186+
return new Container<>(new Bar());
187+
}
188+
167189
}
168190

169191
static class Bar {
170192

171193
}
172194

195+
static class Container<T> {
196+
197+
private final T item;
198+
199+
Container(T item) {
200+
this.item = item;
201+
}
202+
203+
public T getItem() {
204+
return this.item;
205+
}
206+
207+
}
208+
173209
}

0 commit comments

Comments
 (0)