Consider @Import classes as lite @Configuration
Allow classes that are annotated with @Import to be considered as 'lite' @Configuration candidates. Allows the AnnotationConfigApplicationContext to directly register @Import beans even if they are not @Components. Issue: SPR-10533
This commit is contained in:
@@ -101,9 +101,12 @@ abstract class ConfigurationClassUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isLiteConfigurationCandidate(AnnotationMetadata metadata) {
|
public static boolean isLiteConfigurationCandidate(AnnotationMetadata metadata) {
|
||||||
return !metadata.isInterface() && // not an interface or an annotation
|
if(metadata.isInterface()) {
|
||||||
(metadata.isAnnotated(Component.class.getName()) ||
|
return false; // do not consider an interface or an annotation
|
||||||
metadata.hasAnnotatedMethods(Bean.class.getName()));
|
}
|
||||||
|
return metadata.isAnnotated(Import.class.getName()) ||
|
||||||
|
metadata.isAnnotated(Component.class.getName()) ||
|
||||||
|
metadata.hasAnnotatedMethods(Bean.class.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -25,12 +25,15 @@ import java.lang.annotation.RetentionPolicy;
|
|||||||
import java.lang.annotation.Target;
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.tests.sample.beans.TestBean;
|
import org.springframework.tests.sample.beans.TestBean;
|
||||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.Import;
|
import org.springframework.context.annotation.Import;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests that @Import may be used both as a locally declared and meta-declared
|
* Tests that @Import may be used both as a locally declared and meta-declared
|
||||||
* annotation, that all declarations are processed, and that any local declaration
|
* annotation, that all declarations are processed, and that any local declaration
|
||||||
@@ -70,6 +73,16 @@ public class ImportAnnotationDetectionTests {
|
|||||||
assertThat(ctx.getBean("testBean2", TestBean.class).getName(), is("2a"));
|
assertThat(ctx.getBean("testBean2", TestBean.class).getName(), is("2a"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void importFromBean() throws Exception {
|
||||||
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||||
|
ctx.register(ImportFromBean.class);
|
||||||
|
ctx.refresh();
|
||||||
|
assertThat(ctx.containsBean("importAnnotationDetectionTests.ImportFromBean"), is(true));
|
||||||
|
assertThat(ctx.containsBean("testBean1"), is(true));
|
||||||
|
assertThat(ctx.getBean("testBean1", TestBean.class).getName(), is("1"));
|
||||||
|
}
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@MetaImport1
|
@MetaImport1
|
||||||
@MetaImport2
|
@MetaImport2
|
||||||
@@ -136,4 +149,9 @@ public class ImportAnnotationDetectionTests {
|
|||||||
return new TestBean("3");
|
return new TestBean("3");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@MetaImport1
|
||||||
|
static class ImportFromBean {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user