Restrict nested configuration class introspection to component types

Issue: SPR-16839
This commit is contained in:
Juergen Hoeller
2018-09-12 14:02:45 +02:00
parent 45936f0ddd
commit b83bc39ecd
3 changed files with 13 additions and 43 deletions

View File

@@ -76,6 +76,7 @@ import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.AssignableTypeFilter;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.CollectionUtils;
@@ -261,8 +262,10 @@ class ConfigurationClassParser {
protected final SourceClass doProcessConfigurationClass(ConfigurationClass configClass, SourceClass sourceClass)
throws IOException {
// Recursively process any member (nested) classes first
processMemberClasses(configClass, sourceClass);
if (sourceClass.getMetadata().isAnnotated(Component.class.getName())) {
// Recursively process any member (nested) classes first
processMemberClasses(configClass, sourceClass);
}
// Process any @PropertySource annotations
for (AnnotationAttributes propertySource : AnnotationConfigUtils.attributesForRepeatable(

View File

@@ -119,7 +119,7 @@ abstract class ConfigurationClassUtils {
beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_LITE);
}
else {
return hasNestedConfigurationClass(metadata, metadataReaderFactory);
return false;
}
// It's a full or lite configuration candidate... Let's determine the order value, if any.
@@ -131,40 +131,6 @@ abstract class ConfigurationClassUtils {
return true;
}
/**
* Check whether the specified class declares a nested configuration class.
*/
private static boolean hasNestedConfigurationClass(
AnnotationMetadata metadata, MetadataReaderFactory metadataReaderFactory) {
// Potentially nested configuration classes...
if (metadata instanceof StandardAnnotationMetadata) {
Class<?> beanClass = ((StandardAnnotationMetadata) metadata).getIntrospectedClass();
for (Class<?> memberClass : beanClass.getDeclaredClasses()) {
if (isConfigurationCandidate(new StandardAnnotationMetadata(memberClass))) {
return true;
}
}
}
else {
for (String memberName : metadata.getMemberClassNames()) {
try {
MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(memberName);
if (isConfigurationCandidate(metadataReader.getAnnotationMetadata())) {
return true;
}
}
catch (IOException ex) {
if (logger.isDebugEnabled()) {
logger.debug("Could not find class file for introspecting configuration annotations: " +
memberName, ex);
}
}
}
}
return false;
}
/**
* Check the given metadata for a configuration class candidate
* (or nested component class declared within a configuration/component class).