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
*/
protected boolean checkConfigurationClassCandidate(BeanDefinition beanDef) {
AnnotationMetadata metadata;
AnnotationMetadata metadata = null;
// Check already loaded Class if present...
// since we possibly can't even load the class file for this Class.
@ -175,29 +175,31 @@ public class ConfigurationClassPostProcessor implements BeanFactoryPostProcessor
}
else {
String className = beanDef.getBeanClassName();
try {
MetadataReader metadataReader = this.metadataReaderFactory.getMetadataReader(className);
metadata = metadataReader.getAnnotationMetadata();
}
catch (IOException ex) {
if (logger.isDebugEnabled()) {
logger.debug("Could not find class file for introspecting factory methods: " + className, ex);
if (className != null) {
try {
MetadataReader metadataReader = this.metadataReaderFactory.getMetadataReader(className);
metadata = metadataReader.getAnnotationMetadata();
}
catch (IOException ex) {
if (logger.isDebugEnabled()) {
logger.debug("Could not find class file for introspecting factory methods: " + className, ex);
}
return false;
}
return false;
}
}
if (metadata.hasAnnotation(Configuration.class.getName())) {
beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_FULL);
return true;
}
else if (metadata.hasAnnotation(Component.class.getName())) {
beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_LITE);
return true;
}
else {
return false;
if (metadata != null) {
if (metadata.hasAnnotation(Configuration.class.getName())) {
beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_FULL);
return true;
}
else if (metadata.hasAnnotation(Component.class.getName())) {
beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_LITE);
return true;
}
}
return false;
}
/**

View File

@ -18,7 +18,9 @@ package org.springframework.context.annotation;
import static org.junit.Assert.*;
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.RootBeanDefinition;
@ -42,8 +44,8 @@ public class ConfigurationClassPostProcessorTests {
beanFactory.registerBeanDefinition("config", new RootBeanDefinition(SingletonBeanConfig.class));
ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
pp.postProcessBeanFactory(beanFactory);
Foo foo = (Foo) beanFactory.getBean("foo");
Bar bar = (Bar) beanFactory.getBean("bar");
Foo foo = beanFactory.getBean("foo", Foo.class);
Bar bar = beanFactory.getBean("bar", Bar.class);
assertSame(foo, bar.foo);
}
@ -63,6 +65,23 @@ public class ConfigurationClassPostProcessorTests {
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
static class SingletonBeanConfig {