Browse Source

Merge branch 'backport-SPR-9925' into 3.1.x

* backport-SPR-9925:
  Backport "Polish @Imports search code"
  Backport "Ensure @Imports are processed in correct order"
  Backport "Prevent duplicate @Import processing"
  Backport "Polish Javadoc for @Import"
3.1.x
Chris Beams 13 years ago
parent
commit
d547570bcd
  1. 74
      org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java
  2. 5
      org.springframework.context/src/main/java/org/springframework/context/annotation/Import.java
  3. 53
      org.springframework.context/src/test/java/org/springframework/context/annotation/ImportAwareTests.java

74
org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java

@ -16,15 +16,16 @@
package org.springframework.context.annotation; package org.springframework.context.annotation;
import static org.springframework.context.annotation.MetadataUtils.attributesFor;
import java.io.IOException; import java.io.IOException;
import java.lang.annotation.Annotation; import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.Stack; import java.util.Stack;
@ -51,8 +52,6 @@ import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.AssignableTypeFilter; import org.springframework.core.type.filter.AssignableTypeFilter;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import static org.springframework.context.annotation.MetadataUtils.*;
/** /**
* Parses a {@link Configuration} class definition, populating a collection of * Parses a {@link Configuration} class definition, populating a collection of
* {@link ConfigurationClass} objects (parsing a single Configuration class may result in * {@link ConfigurationClass} objects (parsing a single Configuration class may result in
@ -221,10 +220,9 @@ class ConfigurationClassParser {
} }
// process any @Import annotations // process any @Import annotations
List<AnnotationAttributes> imports = Set<String> imports = getImports(metadata.getClassName(), null, new HashSet<String>());
findAllAnnotationAttributes(Import.class, metadata.getClassName(), true); if (imports != null && !imports.isEmpty()) {
for (AnnotationAttributes importAnno : imports) { processImport(configClass, imports.toArray(new String[imports.size()]), true);
processImport(configClass, importAnno.getStringArray("value"), true);
} }
// process any @ImportResource annotations // process any @ImportResource annotations
@ -274,45 +272,36 @@ class ConfigurationClassParser {
} }
/** /**
* Return a list of attribute maps for all declarations of the given annotation * Recursively collect all declared {@code @Import} values. Unlike most
* on the given annotated class using the given MetadataReaderFactory to introspect * meta-annotations it is valid to have several {@code @Import}s declared with
* annotation metadata. Meta-annotations are ordered first in the list, and if the * different values, the usual process or returning values from the first
* target annotation is declared directly on the class, its map of attributes will be * meta-annotation on a class is not sufficient.
* ordered last in the list. * <p>For example, it is common for a {@code @Configuration} class to declare direct
* @param targetAnnotation the annotation to search for, both locally and as a meta-annotation * {@code @Import}s in addition to meta-imports originating from an {@code @Enable}
* @param annotatedClassName the class to inspect * annotation.
* @param classValuesAsString whether class attributes should be returned as strings * @param className the class name to search
* @param imports the imports collected so far or {@code null}
* @param visited used to track visited classes to prevent infinite recursion (must not be null)
* @return a set of all {@link Import#value() import values} or {@code null}
* @throws IOException if there is any problem reading metadata from the named class
*/ */
private List<AnnotationAttributes> findAllAnnotationAttributes( private Set<String> getImports(String className, Set<String> imports,
Class<? extends Annotation> targetAnnotation, String annotatedClassName, Set<String> visited) throws IOException {
boolean classValuesAsString) throws IOException { if (visited.add(className)) {
AnnotationMetadata metadata = metadataReaderFactory.getMetadataReader(className).getAnnotationMetadata();
List<AnnotationAttributes> allAttribs = new ArrayList<AnnotationAttributes>();
MetadataReader reader = this.metadataReaderFactory.getMetadataReader(annotatedClassName);
AnnotationMetadata metadata = reader.getAnnotationMetadata();
String targetAnnotationType = targetAnnotation.getName();
for (String annotationType : metadata.getAnnotationTypes()) { for (String annotationType : metadata.getAnnotationTypes()) {
if (annotationType.equals(targetAnnotationType)) { imports = getImports(annotationType, imports, visited);
continue;
} }
AnnotationMetadata metaAnnotations = Map<String, Object> attributes = metadata.getAnnotationAttributes(Import.class.getName(), true);
this.metadataReaderFactory.getMetadataReader(annotationType).getAnnotationMetadata(); if (attributes != null) {
AnnotationAttributes targetAttribs = String[] value = (String[]) attributes.get("value");
AnnotationAttributes.fromMap(metaAnnotations.getAnnotationAttributes(targetAnnotationType, classValuesAsString)); if (value != null && value.length > 0) {
if (targetAttribs != null) { imports = (imports == null ? new LinkedHashSet<String>() : imports);
allAttribs.add(targetAttribs); imports.addAll(Arrays.asList(value));
} }
} }
AnnotationAttributes localAttribs =
AnnotationAttributes.fromMap(metadata.getAnnotationAttributes(targetAnnotationType, classValuesAsString));
if (localAttribs != null) {
allAttribs.add(localAttribs);
} }
return imports;
return allAttribs;
} }
private void processImport(ConfigurationClass configClass, String[] classesToImport, boolean checkForCircularImports) throws IOException { private void processImport(ConfigurationClass configClass, String[] classesToImport, boolean checkForCircularImports) throws IOException {
@ -451,5 +440,4 @@ class ConfigurationClassParser {
new Location(importStack.peek().getResource(), metadata)); new Location(importStack.peek().getResource(), metadata));
} }
} }
} }

5
org.springframework.context/src/main/java/org/springframework/context/annotation/Import.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2011 the original author or authors. * Copyright 2002-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -53,7 +53,8 @@ import java.lang.annotation.Target;
public @interface Import { public @interface Import {
/** /**
* The @{@link Configuration} and/or {@link ImportSelector} classes to import. * The @{@link Configuration}, {@link ImportSelector} and/or
* {@link ImportBeanDefinitionRegistrar} classes to import.
*/ */
Class<?>[] value(); Class<?>[] value();
} }

53
org.springframework.context/src/test/java/org/springframework/context/annotation/ImportAwareTests.java

@ -25,10 +25,14 @@ import org.junit.Test;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata; import org.springframework.core.type.AnnotationMetadata;
import org.springframework.scheduling.annotation.AsyncAnnotationBeanPostProcessor; import org.springframework.scheduling.annotation.AsyncAnnotationBeanPostProcessor;
import org.springframework.util.Assert;
import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@ -77,6 +81,24 @@ public class ImportAwareTests {
assertThat(foo, is("xyz")); assertThat(foo, is("xyz"));
} }
@Test
public void importRegistrar() throws Exception {
ImportedRegistrar.called = false;
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(ImportingRegistrarConfig.class);
ctx.refresh();
assertNotNull(ctx.getBean("registrarImportedBean"));
}
@Test
public void importRegistrarWithImport() throws Exception {
ImportedRegistrar.called = false;
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(ImportingRegistrarConfigWithImport.class);
ctx.refresh();
assertNotNull(ctx.getBean("registrarImportedBean"));
assertNotNull(ctx.getBean(ImportedConfig.class));
}
@Configuration @Configuration
@Import(ImportedConfig.class) @Import(ImportedConfig.class)
@ -131,4 +153,35 @@ public class ImportAwareTests {
public void setBeanFactory(BeanFactory beanFactory) throws BeansException { public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
} }
} }
@Configuration
@EnableImportRegistrar
static class ImportingRegistrarConfig {
}
@Configuration
@EnableImportRegistrar
@Import(ImportedConfig.class)
static class ImportingRegistrarConfigWithImport {
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(ImportedRegistrar.class)
public @interface EnableImportRegistrar {
}
static class ImportedRegistrar implements ImportBeanDefinitionRegistrar {
static boolean called;
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
BeanDefinitionRegistry registry) {
BeanDefinition beanDefinition = new GenericBeanDefinition();
beanDefinition.setBeanClassName(String.class.getName());
registry.registerBeanDefinition("registrarImportedBean", beanDefinition );
Assert.state(called == false, "ImportedRegistrar called twice");
called = true;
}
}
} }

Loading…
Cancel
Save