Skip to content

Commit 636d1db

Browse files
committed
avoid NPE for definitions without bean class specified
1 parent b88edd1 commit 636d1db

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-21
lines changed

org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public void processConfigBeanDefinitions(BeanDefinitionRegistry registry) {
166166
* @return whether the candidate qualifies as (any kind of) configuration class
167167
*/
168168
protected boolean checkConfigurationClassCandidate(BeanDefinition beanDef) {
169-
AnnotationMetadata metadata;
169+
AnnotationMetadata metadata = null;
170170

171171
// Check already loaded Class if present...
172172
// since we possibly can't even load the class file for this Class.
@@ -175,29 +175,31 @@ protected boolean checkConfigurationClassCandidate(BeanDefinition beanDef) {
175175
}
176176
else {
177177
String className = beanDef.getBeanClassName();
178-
try {
179-
MetadataReader metadataReader = this.metadataReaderFactory.getMetadataReader(className);
180-
metadata = metadataReader.getAnnotationMetadata();
181-
}
182-
catch (IOException ex) {
183-
if (logger.isDebugEnabled()) {
184-
logger.debug("Could not find class file for introspecting factory methods: " + className, ex);
178+
if (className != null) {
179+
try {
180+
MetadataReader metadataReader = this.metadataReaderFactory.getMetadataReader(className);
181+
metadata = metadataReader.getAnnotationMetadata();
182+
}
183+
catch (IOException ex) {
184+
if (logger.isDebugEnabled()) {
185+
logger.debug("Could not find class file for introspecting factory methods: " + className, ex);
186+
}
187+
return false;
185188
}
186-
return false;
187189
}
188190
}
189191

190-
if (metadata.hasAnnotation(Configuration.class.getName())) {
191-
beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_FULL);
192-
return true;
193-
}
194-
else if (metadata.hasAnnotation(Component.class.getName())) {
195-
beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_LITE);
196-
return true;
197-
}
198-
else {
199-
return false;
192+
if (metadata != null) {
193+
if (metadata.hasAnnotation(Configuration.class.getName())) {
194+
beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_FULL);
195+
return true;
196+
}
197+
else if (metadata.hasAnnotation(Component.class.getName())) {
198+
beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_LITE);
199+
return true;
200+
}
200201
}
202+
return false;
201203
}
202204

203205
/**

org.springframework.context/src/test/java/org/springframework/context/annotation/ConfigurationClassPostProcessorTests.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
import static org.junit.Assert.*;
2020
import org.junit.Test;
21+
import test.beans.TestBean;
2122

23+
import org.springframework.beans.factory.support.ChildBeanDefinition;
2224
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
2325
import org.springframework.beans.factory.support.RootBeanDefinition;
2426

@@ -42,8 +44,8 @@ public void testEnhancementIsPresentBecauseSingletonSemanticsAreRespected() {
4244
beanFactory.registerBeanDefinition("config", new RootBeanDefinition(SingletonBeanConfig.class));
4345
ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
4446
pp.postProcessBeanFactory(beanFactory);
45-
Foo foo = (Foo) beanFactory.getBean("foo");
46-
Bar bar = (Bar) beanFactory.getBean("bar");
47+
Foo foo = beanFactory.getBean("foo", Foo.class);
48+
Bar bar = beanFactory.getBean("bar", Bar.class);
4749
assertSame(foo, bar.foo);
4850
}
4951

@@ -63,6 +65,23 @@ public void testAlreadyLoadedConfigurationClasses() {
6365
beanFactory.getBean("bar");
6466
}
6567

68+
/**
69+
* Tests whether a bean definition without a specified bean class is handled
70+
* correctly.
71+
*/
72+
@Test
73+
public void testPostProcessorIntrospectsInheritedDefinitionsCorrectly() {
74+
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
75+
beanFactory.registerBeanDefinition("config", new RootBeanDefinition(SingletonBeanConfig.class));
76+
beanFactory.registerBeanDefinition("parent", new RootBeanDefinition(TestBean.class));
77+
beanFactory.registerBeanDefinition("child", new ChildBeanDefinition("parent"));
78+
ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
79+
pp.postProcessBeanFactory(beanFactory);
80+
Foo foo = beanFactory.getBean("foo", Foo.class);
81+
Bar bar = beanFactory.getBean("bar", Bar.class);
82+
assertSame(foo, bar.foo);
83+
}
84+
6685

6786
@Configuration
6887
static class SingletonBeanConfig {

0 commit comments

Comments
 (0)