From 680afa75d838c8377238e01033e692c5735fe01b Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Sat, 14 Jul 2018 01:02:39 +0200 Subject: [PATCH] ListableBeanFactory.getBeansOfType does not include null bean entries Issue: SPR-17034 --- .../factory/support/DefaultListableBeanFactory.java | 4 +++- .../annotation/WebMvcConfigurationSupportTests.java | 9 ++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java index d34b6af0ea38..4077477ac6d0 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java @@ -512,7 +512,9 @@ public Map getBeansOfType(@Nullable Class type, boolean includ for (String beanName : beanNames) { try { Object beanInstance = getBean(beanName); - result.put(beanName, (beanInstance instanceof NullBean ? null : (T) beanInstance)); + if (!(beanInstance instanceof NullBean)) { + result.put(beanName, (T) beanInstance); + } } catch (BeanCreationException ex) { Throwable rootCause = ex.getMostSpecificCause(); diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupportTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupportTests.java index 0df8f7a430f4..d7aacfcc8554 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupportTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupportTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,6 +18,7 @@ import java.util.List; import java.util.Locale; +import java.util.Map; import javax.servlet.http.HttpServletRequest; import com.fasterxml.jackson.databind.ObjectMapper; @@ -66,6 +67,7 @@ import org.springframework.web.method.support.ModelAndViewContainer; import org.springframework.web.servlet.HandlerExceptionResolver; import org.springframework.web.servlet.HandlerExecutionChain; +import org.springframework.web.servlet.HandlerMapping; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping; import org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor; @@ -121,6 +123,11 @@ public void requestMappingHandlerMapping() throws Exception { public void emptyHandlerMappings() { ApplicationContext context = initContext(WebConfig.class); + Map handlerMappings = context.getBeansOfType(HandlerMapping.class); + assertFalse(handlerMappings.containsKey("viewControllerHandlerMapping")); + assertFalse(handlerMappings.containsKey("resourceHandlerMapping")); + assertFalse(handlerMappings.containsKey("defaultServletHandlerMapping")); + Object nullBean = context.getBean("viewControllerHandlerMapping"); assertTrue(nullBean.equals(null));