Enforce non-null value from getBean and at injection points

Bean-derived null values may still get passed into bean properties and injection points but only if those are declared as non-required. Note that getBean will never return null; a manual bean.equals(null) / "null".equals(bean.toString()) check identifies expected null values now.  This will only ever happen with custom FactoryBeans or factory methods returning null - and since all common cases are handled by autowiring or bean property values in bean definitions, there should be no need to ever manually check for such a null value received from getBean.

Issue: SPR-15829
This commit is contained in:
Juergen Hoeller
2017-08-18 00:11:35 +02:00
parent 10dcaa9bf6
commit b94302b5bd
30 changed files with 344 additions and 204 deletions

View File

@@ -58,14 +58,14 @@ public class CacheResolverCustomizationTests {
@Before
public void setUp() {
public void setup() {
ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
this.cacheManager = context.getBean("cacheManager", CacheManager.class);
this.anotherCacheManager = context.getBean("anotherCacheManager", CacheManager.class);
this.simpleService = context.getBean(SimpleService.class);
}
@Test
public void noCustomization() {
Cache cache = this.cacheManager.getCache("default");
@@ -162,12 +162,6 @@ public class CacheResolverCustomizationTests {
return CacheTestUtils.createSimpleCacheManager("default", "primary", "secondary");
}
@Override
@Bean
public KeyGenerator keyGenerator() {
return null;
}
@Bean
public CacheManager anotherCacheManager() {
return CacheTestUtils.createSimpleCacheManager("default", "primary", "secondary");

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2017 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.
@@ -114,7 +114,7 @@ public class CommonAnnotationBeanPostProcessorTests {
rbd.setFactoryMethodName("create");
bf.registerBeanDefinition("bean", rbd);
assertNull(bf.getBean("bean"));
assertEquals("null", bf.getBean("bean").toString());
bf.destroySingletons();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 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.
@@ -30,13 +30,6 @@ import static org.junit.Assert.*;
*/
public class GenericApplicationContextTests {
@Test
public void nullBeanRegistration() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
bf.registerSingleton("nullBean", null);
new GenericApplicationContext(bf).refresh();
}
@Test
public void getBeanForClass() {
GenericApplicationContext ac = new GenericApplicationContext();