AnnotationMetadata returns Class values by default (again), allowing for explicit retrieval of String class names where preferred (SPR-5827)
This commit is contained in:
@@ -24,7 +24,6 @@ import java.util.Set;
|
||||
import org.springframework.beans.factory.parsing.Location;
|
||||
import org.springframework.beans.factory.parsing.Problem;
|
||||
import org.springframework.beans.factory.parsing.ProblemReporter;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionReader;
|
||||
import org.springframework.core.io.DescriptiveResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
@@ -51,7 +50,7 @@ final class ConfigurationClass {
|
||||
|
||||
private String beanName;
|
||||
|
||||
private final Map<String, String> importedResources = new LinkedHashMap<String, String>();
|
||||
private final Map<String, Class> importedResources = new LinkedHashMap<String, Class>();
|
||||
|
||||
private final Set<ConfigurationClassMethod> methods = new LinkedHashSet<ConfigurationClassMethod>();
|
||||
|
||||
@@ -108,11 +107,11 @@ final class ConfigurationClass {
|
||||
return this.methods;
|
||||
}
|
||||
|
||||
public void addImportedResource(String importedResource, String readerClassName) {
|
||||
this.importedResources.put(importedResource, readerClassName);
|
||||
public void addImportedResource(String importedResource, Class readerClass) {
|
||||
this.importedResources.put(importedResource, readerClass);
|
||||
}
|
||||
|
||||
public Map<String, String> getImportedResources() {
|
||||
public Map<String, Class> getImportedResources() {
|
||||
return this.importedResources;
|
||||
}
|
||||
|
||||
|
||||
@@ -26,6 +26,7 @@ import java.util.Set;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
|
||||
import org.springframework.beans.factory.annotation.Autowire;
|
||||
import org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor;
|
||||
@@ -41,8 +42,6 @@ import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
import org.springframework.core.type.MethodMetadata;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
@@ -214,28 +213,24 @@ class ConfigurationClassBeanDefinitionReader {
|
||||
registry.registerBeanDefinition(beanName, beanDefToRegister);
|
||||
}
|
||||
|
||||
private void loadBeanDefinitionsFromImportedResources(Map<String, String> importedResources) {
|
||||
|
||||
HashMap<String, BeanDefinitionReader> readerInstanceCache = new HashMap<String, BeanDefinitionReader>();
|
||||
|
||||
for (String resource : importedResources.keySet()) {
|
||||
String readerClassName = importedResources.get(resource);
|
||||
|
||||
if (!readerInstanceCache.containsKey(readerClassName)) {
|
||||
private void loadBeanDefinitionsFromImportedResources(Map<String, Class> importedResources) {
|
||||
Map<Class, BeanDefinitionReader> readerInstanceCache = new HashMap<Class, BeanDefinitionReader>();
|
||||
for (Map.Entry<String, Class> entry : importedResources.entrySet()) {
|
||||
String resource = entry.getKey();
|
||||
Class readerClass = entry.getValue();
|
||||
if (!readerInstanceCache.containsKey(readerClass)) {
|
||||
try {
|
||||
@SuppressWarnings("unchecked")
|
||||
Class<? extends BeanDefinitionReader> readerClass =
|
||||
(Class<? extends BeanDefinitionReader>) ClassUtils.forName(readerClassName, ClassUtils.getDefaultClassLoader());
|
||||
BeanDefinitionReader readerInstance = readerClass.getConstructor(BeanDefinitionRegistry.class).newInstance(this.registry);
|
||||
readerInstanceCache.put(readerClassName, readerInstance);
|
||||
} catch (Exception ex) {
|
||||
ReflectionUtils.handleReflectionException(ex);
|
||||
BeanDefinitionReader readerInstance = (BeanDefinitionReader)
|
||||
readerClass.getConstructor(BeanDefinitionRegistry.class).newInstance(this.registry);
|
||||
readerInstanceCache.put(readerClass, readerInstance);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new IllegalStateException("Could not instantiate BeanDefinitionReader class [" + readerClass.getName() + "]");
|
||||
}
|
||||
}
|
||||
|
||||
BeanDefinitionReader reader = readerInstanceCache.get(readerClassName);
|
||||
BeanDefinitionReader reader = readerInstanceCache.get(readerClass);
|
||||
// TODO SPR-6310: qualify relatively pathed locations as done in AbstractContextLoader.modifyLocations
|
||||
reader.loadBeanDefinitions(importedResources.keySet().toArray(new String[]{}));
|
||||
reader.loadBeanDefinitions(resource);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -28,14 +28,13 @@ import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.parsing.Location;
|
||||
import org.springframework.beans.factory.parsing.Problem;
|
||||
import org.springframework.beans.factory.parsing.ProblemReporter;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionReader;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
import org.springframework.core.type.MethodMetadata;
|
||||
import org.springframework.core.type.StandardAnnotationMetadata;
|
||||
import org.springframework.core.type.classreading.MetadataReader;
|
||||
import org.springframework.core.type.classreading.MetadataReaderFactory;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Parses a {@link Configuration} class definition, populating a model (collection) of
|
||||
@@ -128,12 +127,17 @@ class ConfigurationClassParser {
|
||||
|
||||
protected void doProcessConfigurationClass(ConfigurationClass configClass, AnnotationMetadata metadata) throws IOException {
|
||||
if (metadata.isAnnotated(Import.class.getName())) {
|
||||
processImport(configClass, (String[]) metadata.getAnnotationAttributes(Import.class.getName()).get("value"));
|
||||
processImport(configClass, (String[]) metadata.getAnnotationAttributes(Import.class.getName(), true).get("value"));
|
||||
}
|
||||
if (metadata.isAnnotated(ImportResource.class.getName())) {
|
||||
String readerClassName = (String) metadata.getAnnotationAttributes(ImportResource.class.getName()).get("reader");
|
||||
for (String importedResource : (String[]) metadata.getAnnotationAttributes(ImportResource.class.getName()).get("value")) {
|
||||
configClass.addImportedResource(importedResource, readerClassName);
|
||||
String[] resources = (String[]) metadata.getAnnotationAttributes(ImportResource.class.getName()).get("value");
|
||||
Class readerClass = (Class) metadata.getAnnotationAttributes(ImportResource.class.getName()).get("reader");
|
||||
if (readerClass == null) {
|
||||
throw new IllegalStateException("No reader class associated with imported resources: " +
|
||||
StringUtils.arrayToCommaDelimitedString(resources));
|
||||
}
|
||||
for (String resource : resources) {
|
||||
configClass.addImportedResource(resource, readerClass);
|
||||
}
|
||||
}
|
||||
Set<MethodMetadata> methods = metadata.getAnnotatedMethods(Bean.class.getName());
|
||||
|
||||
Reference in New Issue
Block a user