diff --git a/org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java b/org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java index c3628affbc5..bd93db8671b 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java +++ b/org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java @@ -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; } /** diff --git a/org.springframework.context/src/test/java/org/springframework/context/annotation/ConfigurationClassPostProcessorTests.java b/org.springframework.context/src/test/java/org/springframework/context/annotation/ConfigurationClassPostProcessorTests.java index a62e8b113fa..d3800a9d10c 100644 --- a/org.springframework.context/src/test/java/org/springframework/context/annotation/ConfigurationClassPostProcessorTests.java +++ b/org.springframework.context/src/test/java/org/springframework/context/annotation/ConfigurationClassPostProcessorTests.java @@ -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 {