Optimized ImportAware processing to avoid re-reading class files
This commit is contained in:
parent
009e362709
commit
519f78c3f5
|
|
@ -424,8 +424,7 @@ class ConfigurationClassParser {
|
|||
}
|
||||
else {
|
||||
// candidate class not an ImportSelector or ImportBeanDefinitionRegistrar -> process it as a @Configuration class
|
||||
this.importStack.registerImport(importingClassMetadata.getClassName(),
|
||||
candidate.getMetadata().getClassName());
|
||||
this.importStack.registerImport(importingClassMetadata, candidate.getMetadata().getClassName());
|
||||
processConfigurationClass(candidate.asConfigClass(configClass));
|
||||
}
|
||||
}
|
||||
|
|
@ -558,7 +557,7 @@ class ConfigurationClassParser {
|
|||
|
||||
interface ImportRegistry {
|
||||
|
||||
String getImportingClassFor(String importedClass);
|
||||
AnnotationMetadata getImportingClassFor(String importedClass);
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -566,14 +565,14 @@ class ConfigurationClassParser {
|
|||
@SuppressWarnings("serial")
|
||||
private static class ImportStack extends Stack<ConfigurationClass> implements ImportRegistry {
|
||||
|
||||
private final Map<String, String> imports = new HashMap<String, String>();
|
||||
private final Map<String, AnnotationMetadata> imports = new HashMap<String, AnnotationMetadata>();
|
||||
|
||||
public void registerImport(String importingClass, String importedClass) {
|
||||
public void registerImport(AnnotationMetadata importingClass, String importedClass) {
|
||||
this.imports.put(importedClass, importingClass);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getImportingClassFor(String importedClass) {
|
||||
public AnnotationMetadata getImportingClassFor(String importedClass) {
|
||||
return this.imports.get(importedClass);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -17,7 +17,6 @@
|
|||
package org.springframework.context.annotation;
|
||||
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.io.IOException;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
|
|
@ -65,7 +64,6 @@ import org.springframework.core.io.ResourceLoader;
|
|||
import org.springframework.core.type.AnnotationMetadata;
|
||||
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
|
||||
import org.springframework.core.type.classreading.MetadataReaderFactory;
|
||||
import org.springframework.core.type.classreading.SimpleMetadataReaderFactory;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
|
|
@ -379,38 +377,27 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo
|
|||
}
|
||||
|
||||
|
||||
private static class ImportAwareBeanPostProcessor implements BeanPostProcessor, PriorityOrdered, BeanFactoryAware {
|
||||
private static class ImportAwareBeanPostProcessor implements BeanPostProcessor, BeanFactoryAware, PriorityOrdered {
|
||||
|
||||
private BeanFactory beanFactory;
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return Ordered.HIGHEST_PRECEDENCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBeanFactory(BeanFactory beanFactory) {
|
||||
this.beanFactory = beanFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return Ordered.HIGHEST_PRECEDENCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object postProcessBeforeInitialization(Object bean, String beanName) {
|
||||
if (bean instanceof ImportAware) {
|
||||
ImportRegistry importRegistry = this.beanFactory.getBean(IMPORT_REGISTRY_BEAN_NAME, ImportRegistry.class);
|
||||
String importingClass = importRegistry.getImportingClassFor(bean.getClass().getSuperclass().getName());
|
||||
AnnotationMetadata importingClass = importRegistry.getImportingClassFor(bean.getClass().getSuperclass().getName());
|
||||
if (importingClass != null) {
|
||||
try {
|
||||
AnnotationMetadata metadata =
|
||||
new SimpleMetadataReaderFactory().getMetadataReader(importingClass).getAnnotationMetadata();
|
||||
((ImportAware) bean).setImportMetadata(metadata);
|
||||
}
|
||||
catch (IOException ex) {
|
||||
// should never occur -> at this point we know the class is present anyway
|
||||
throw new IllegalStateException(ex);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// no importing class was found
|
||||
((ImportAware) bean).setImportMetadata(importingClass);
|
||||
}
|
||||
}
|
||||
return bean;
|
||||
|
|
|
|||
Loading…
Reference in New Issue