avoid NPE for definitions without bean class specified
This commit is contained in:
parent
b88edd1dfc
commit
636d1db36d
|
|
@ -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,29 +175,31 @@ public class ConfigurationClassPostProcessor implements BeanFactoryPostProcessor
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
String className = beanDef.getBeanClassName();
|
String className = beanDef.getBeanClassName();
|
||||||
try {
|
if (className != null) {
|
||||||
MetadataReader metadataReader = this.metadataReaderFactory.getMetadataReader(className);
|
try {
|
||||||
metadata = metadataReader.getAnnotationMetadata();
|
MetadataReader metadataReader = this.metadataReaderFactory.getMetadataReader(className);
|
||||||
}
|
metadata = metadataReader.getAnnotationMetadata();
|
||||||
catch (IOException ex) {
|
}
|
||||||
if (logger.isDebugEnabled()) {
|
catch (IOException ex) {
|
||||||
logger.debug("Could not find class file for introspecting factory methods: " + className, 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())) {
|
if (metadata != null) {
|
||||||
beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_FULL);
|
if (metadata.hasAnnotation(Configuration.class.getName())) {
|
||||||
return true;
|
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);
|
else if (metadata.hasAnnotation(Component.class.getName())) {
|
||||||
return true;
|
beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_LITE);
|
||||||
}
|
return true;
|
||||||
else {
|
}
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -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 {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue