avoid NPE for definitions without bean class specified

This commit is contained in:
Juergen Hoeller 2009-05-05 18:33:20 +00:00
parent b88edd1dfc
commit 636d1db36d
2 changed files with 42 additions and 21 deletions

View File

@ -166,7 +166,7 @@ public class ConfigurationClassPostProcessor implements BeanFactoryPostProcessor
* @return whether the candidate qualifies as (any kind of) configuration class * @return whether the candidate qualifies as (any kind of) configuration class
*/ */
protected boolean checkConfigurationClassCandidate(BeanDefinition beanDef) { protected boolean checkConfigurationClassCandidate(BeanDefinition beanDef) {
AnnotationMetadata metadata; AnnotationMetadata metadata = null;
// Check already loaded Class if present... // Check already loaded Class if present...
// since we possibly can't even load the class file for this Class. // since we possibly can't even load the class file for this Class.
@ -175,6 +175,7 @@ public class ConfigurationClassPostProcessor implements BeanFactoryPostProcessor
} }
else { else {
String className = beanDef.getBeanClassName(); String className = beanDef.getBeanClassName();
if (className != null) {
try { try {
MetadataReader metadataReader = this.metadataReaderFactory.getMetadataReader(className); MetadataReader metadataReader = this.metadataReaderFactory.getMetadataReader(className);
metadata = metadataReader.getAnnotationMetadata(); metadata = metadataReader.getAnnotationMetadata();
@ -186,7 +187,9 @@ public class ConfigurationClassPostProcessor implements BeanFactoryPostProcessor
return false; return false;
} }
} }
}
if (metadata != null) {
if (metadata.hasAnnotation(Configuration.class.getName())) { if (metadata.hasAnnotation(Configuration.class.getName())) {
beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_FULL); beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_FULL);
return true; return true;
@ -195,9 +198,8 @@ public class ConfigurationClassPostProcessor implements BeanFactoryPostProcessor
beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_LITE); beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_LITE);
return true; return true;
} }
else {
return false;
} }
return false;
} }
/** /**

View File

@ -18,7 +18,9 @@ package org.springframework.context.annotation;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import org.junit.Test; import org.junit.Test;
import test.beans.TestBean;
import org.springframework.beans.factory.support.ChildBeanDefinition;
import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.beans.factory.support.RootBeanDefinition;
@ -42,8 +44,8 @@ public class ConfigurationClassPostProcessorTests {
beanFactory.registerBeanDefinition("config", new RootBeanDefinition(SingletonBeanConfig.class)); beanFactory.registerBeanDefinition("config", new RootBeanDefinition(SingletonBeanConfig.class));
ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor(); ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
pp.postProcessBeanFactory(beanFactory); pp.postProcessBeanFactory(beanFactory);
Foo foo = (Foo) beanFactory.getBean("foo"); Foo foo = beanFactory.getBean("foo", Foo.class);
Bar bar = (Bar) beanFactory.getBean("bar"); Bar bar = beanFactory.getBean("bar", Bar.class);
assertSame(foo, bar.foo); assertSame(foo, bar.foo);
} }
@ -63,6 +65,23 @@ public class ConfigurationClassPostProcessorTests {
beanFactory.getBean("bar"); beanFactory.getBean("bar");
} }
/**
* Tests whether a bean definition without a specified bean class is handled
* correctly.
*/
@Test
public void testPostProcessorIntrospectsInheritedDefinitionsCorrectly() {
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
beanFactory.registerBeanDefinition("config", new RootBeanDefinition(SingletonBeanConfig.class));
beanFactory.registerBeanDefinition("parent", new RootBeanDefinition(TestBean.class));
beanFactory.registerBeanDefinition("child", new ChildBeanDefinition("parent"));
ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
pp.postProcessBeanFactory(beanFactory);
Foo foo = beanFactory.getBean("foo", Foo.class);
Bar bar = beanFactory.getBean("bar", Bar.class);
assertSame(foo, bar.foo);
}
@Configuration @Configuration
static class SingletonBeanConfig { static class SingletonBeanConfig {