Skip to content

Commit 680afa7

Browse files
committed
ListableBeanFactory.getBeansOfType does not include null bean entries
Issue: SPR-17034
1 parent 24a30ba commit 680afa7

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,9 @@ public <T> Map<String, T> getBeansOfType(@Nullable Class<T> type, boolean includ
512512
for (String beanName : beanNames) {
513513
try {
514514
Object beanInstance = getBean(beanName);
515-
result.put(beanName, (beanInstance instanceof NullBean ? null : (T) beanInstance));
515+
if (!(beanInstance instanceof NullBean)) {
516+
result.put(beanName, (T) beanInstance);
517+
}
516518
}
517519
catch (BeanCreationException ex) {
518520
Throwable rootCause = ex.getMostSpecificCause();

spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupportTests.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818

1919
import java.util.List;
2020
import java.util.Locale;
21+
import java.util.Map;
2122
import javax.servlet.http.HttpServletRequest;
2223

2324
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -66,6 +67,7 @@
6667
import org.springframework.web.method.support.ModelAndViewContainer;
6768
import org.springframework.web.servlet.HandlerExceptionResolver;
6869
import org.springframework.web.servlet.HandlerExecutionChain;
70+
import org.springframework.web.servlet.HandlerMapping;
6971
import org.springframework.web.servlet.ViewResolver;
7072
import org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping;
7173
import org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor;
@@ -121,6 +123,11 @@ public void requestMappingHandlerMapping() throws Exception {
121123
public void emptyHandlerMappings() {
122124
ApplicationContext context = initContext(WebConfig.class);
123125

126+
Map<String, HandlerMapping> handlerMappings = context.getBeansOfType(HandlerMapping.class);
127+
assertFalse(handlerMappings.containsKey("viewControllerHandlerMapping"));
128+
assertFalse(handlerMappings.containsKey("resourceHandlerMapping"));
129+
assertFalse(handlerMappings.containsKey("defaultServletHandlerMapping"));
130+
124131
Object nullBean = context.getBean("viewControllerHandlerMapping");
125132
assertTrue(nullBean.equals(null));
126133

0 commit comments

Comments
 (0)