Browse Source

Create ScannedGenericBeanDefinitions for index-derived components as well

Closes gh-24638
pull/24679/head
Juergen Hoeller 6 years ago
parent
commit
bee63e38c1
  1. 6
      spring-context/src/main/java/org/springframework/context/annotation/ClassPathScanningCandidateComponentProvider.java
  2. 66
      spring-context/src/test/java/org/springframework/context/annotation/ClassPathScanningCandidateComponentProviderTests.java

6
spring-context/src/main/java/org/springframework/context/annotation/ClassPathScanningCandidateComponentProvider.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -29,7 +29,6 @@ import org.apache.commons.logging.LogFactory; @@ -29,7 +29,6 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition;
import org.springframework.beans.factory.annotation.Lookup;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
@ -386,8 +385,7 @@ public class ClassPathScanningCandidateComponentProvider implements EnvironmentC @@ -386,8 +385,7 @@ public class ClassPathScanningCandidateComponentProvider implements EnvironmentC
for (String type : types) {
MetadataReader metadataReader = getMetadataReaderFactory().getMetadataReader(type);
if (isCandidateComponent(metadataReader)) {
AnnotatedGenericBeanDefinition sbd = new AnnotatedGenericBeanDefinition(
metadataReader.getAnnotationMetadata());
ScannedGenericBeanDefinition sbd = new ScannedGenericBeanDefinition(metadataReader);
if (isCandidateComponent(sbd)) {
if (debugEnabled) {
logger.debug("Using candidate component class from index: " + type);

66
spring-context/src/test/java/org/springframework/context/annotation/ClassPathScanningCandidateComponentProviderTests.java

@ -41,7 +41,6 @@ import example.scannable.sub.BarComponent; @@ -41,7 +41,6 @@ import example.scannable.sub.BarComponent;
import org.aspectj.lang.annotation.Aspect;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.testfixture.index.CandidateComponentsTestClassLoader;
import org.springframework.core.env.ConfigurableEnvironment;
@ -80,18 +79,17 @@ public class ClassPathScanningCandidateComponentProviderTests { @@ -80,18 +79,17 @@ public class ClassPathScanningCandidateComponentProviderTests {
ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true);
provider.setResourceLoader(new DefaultResourceLoader(
CandidateComponentsTestClassLoader.disableIndex(getClass().getClassLoader())));
testDefault(provider, ScannedGenericBeanDefinition.class);
testDefault(provider);
}
@Test
public void defaultsWithIndex() {
ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true);
provider.setResourceLoader(new DefaultResourceLoader(TEST_BASE_CLASSLOADER));
testDefault(provider, AnnotatedGenericBeanDefinition.class);
testDefault(provider);
}
private void testDefault(ClassPathScanningCandidateComponentProvider provider,
Class<? extends BeanDefinition> expectedBeanDefinitionType) {
private void testDefault(ClassPathScanningCandidateComponentProvider provider) {
Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_BASE_PACKAGE);
assertThat(containsBeanClass(candidates, DefaultNamedComponent.class)).isTrue();
assertThat(containsBeanClass(candidates, NamedComponent.class)).isTrue();
@ -101,7 +99,7 @@ public class ClassPathScanningCandidateComponentProviderTests { @@ -101,7 +99,7 @@ public class ClassPathScanningCandidateComponentProviderTests {
assertThat(containsBeanClass(candidates, ServiceInvocationCounter.class)).isTrue();
assertThat(containsBeanClass(candidates, BarComponent.class)).isTrue();
assertThat(candidates.size()).isEqualTo(7);
assertBeanDefinitionType(candidates, expectedBeanDefinitionType);
assertBeanDefinitionType(candidates);
}
@Test
@ -109,22 +107,21 @@ public class ClassPathScanningCandidateComponentProviderTests { @@ -109,22 +107,21 @@ public class ClassPathScanningCandidateComponentProviderTests {
ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true);
provider.setResourceLoader(new DefaultResourceLoader(
CandidateComponentsTestClassLoader.disableIndex(getClass().getClassLoader())));
testAntStyle(provider, ScannedGenericBeanDefinition.class);
testAntStyle(provider);
}
@Test
public void antStylePackageWithIndex() {
ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true);
provider.setResourceLoader(new DefaultResourceLoader(TEST_BASE_CLASSLOADER));
testAntStyle(provider, AnnotatedGenericBeanDefinition.class);
testAntStyle(provider);
}
private void testAntStyle(ClassPathScanningCandidateComponentProvider provider,
Class<? extends BeanDefinition> expectedBeanDefinitionType) {
private void testAntStyle(ClassPathScanningCandidateComponentProvider provider) {
Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_BASE_PACKAGE + ".**.sub");
assertThat(containsBeanClass(candidates, BarComponent.class)).isTrue();
assertThat(candidates.size()).isEqualTo(1);
assertBeanDefinitionType(candidates, expectedBeanDefinitionType);
assertBeanDefinitionType(candidates);
}
@Test
@ -151,7 +148,7 @@ public class ClassPathScanningCandidateComponentProviderTests { @@ -151,7 +148,7 @@ public class ClassPathScanningCandidateComponentProviderTests {
provider.addIncludeFilter(new AnnotationTypeFilter(Component.class));
provider.resetFilters(true);
Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_BASE_PACKAGE);
assertBeanDefinitionType(candidates, AnnotatedGenericBeanDefinition.class);
assertBeanDefinitionType(candidates);
}
@Test
@ -159,20 +156,19 @@ public class ClassPathScanningCandidateComponentProviderTests { @@ -159,20 +156,19 @@ public class ClassPathScanningCandidateComponentProviderTests {
ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
provider.setResourceLoader(new DefaultResourceLoader(
CandidateComponentsTestClassLoader.disableIndex(getClass().getClassLoader())));
testCustomAnnotationTypeIncludeFilter(provider, ScannedGenericBeanDefinition.class);
testCustomAnnotationTypeIncludeFilter(provider);
}
@Test
public void customAnnotationTypeIncludeFilterWithIndex() {
ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
provider.setResourceLoader(new DefaultResourceLoader(TEST_BASE_CLASSLOADER));
testCustomAnnotationTypeIncludeFilter(provider, AnnotatedGenericBeanDefinition.class);
testCustomAnnotationTypeIncludeFilter(provider);
}
private void testCustomAnnotationTypeIncludeFilter(ClassPathScanningCandidateComponentProvider provider,
Class<? extends BeanDefinition> expectedBeanDefinitionType) {
private void testCustomAnnotationTypeIncludeFilter(ClassPathScanningCandidateComponentProvider provider) {
provider.addIncludeFilter(new AnnotationTypeFilter(Component.class));
testDefault(provider, expectedBeanDefinitionType);
testDefault(provider);
}
@Test
@ -180,18 +176,17 @@ public class ClassPathScanningCandidateComponentProviderTests { @@ -180,18 +176,17 @@ public class ClassPathScanningCandidateComponentProviderTests {
ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
provider.setResourceLoader(new DefaultResourceLoader(
CandidateComponentsTestClassLoader.disableIndex(getClass().getClassLoader())));
testCustomAssignableTypeIncludeFilter(provider, ScannedGenericBeanDefinition.class);
testCustomAssignableTypeIncludeFilter(provider);
}
@Test
public void customAssignableTypeIncludeFilterWithIndex() {
ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
provider.setResourceLoader(new DefaultResourceLoader(TEST_BASE_CLASSLOADER));
testCustomAssignableTypeIncludeFilter(provider, AnnotatedGenericBeanDefinition.class);
testCustomAssignableTypeIncludeFilter(provider);
}
private void testCustomAssignableTypeIncludeFilter(ClassPathScanningCandidateComponentProvider provider,
Class<? extends BeanDefinition> expectedBeanDefinitionType) {
private void testCustomAssignableTypeIncludeFilter(ClassPathScanningCandidateComponentProvider provider) {
provider.addIncludeFilter(new AssignableTypeFilter(FooService.class));
Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_BASE_PACKAGE);
// Interfaces/Abstract class are filtered out automatically.
@ -199,7 +194,7 @@ public class ClassPathScanningCandidateComponentProviderTests { @@ -199,7 +194,7 @@ public class ClassPathScanningCandidateComponentProviderTests {
assertThat(containsBeanClass(candidates, FooServiceImpl.class)).isTrue();
assertThat(containsBeanClass(candidates, ScopedProxyTestBean.class)).isTrue();
assertThat(candidates.size()).isEqualTo(3);
assertBeanDefinitionType(candidates, expectedBeanDefinitionType);
assertBeanDefinitionType(candidates);
}
@Test
@ -207,18 +202,17 @@ public class ClassPathScanningCandidateComponentProviderTests { @@ -207,18 +202,17 @@ public class ClassPathScanningCandidateComponentProviderTests {
ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
provider.setResourceLoader(new DefaultResourceLoader(
CandidateComponentsTestClassLoader.disableIndex(getClass().getClassLoader())));
testCustomSupportedIncludeAndExcludeFilter(provider, ScannedGenericBeanDefinition.class);
testCustomSupportedIncludeAndExcludeFilter(provider);
}
@Test
public void customSupportedIncludeAndExcludeFilterWithIndex() {
ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
provider.setResourceLoader(new DefaultResourceLoader(TEST_BASE_CLASSLOADER));
testCustomSupportedIncludeAndExcludeFilter(provider, AnnotatedGenericBeanDefinition.class);
testCustomSupportedIncludeAndExcludeFilter(provider);
}
private void testCustomSupportedIncludeAndExcludeFilter(ClassPathScanningCandidateComponentProvider provider,
Class<? extends BeanDefinition> expectedBeanDefinitionType) {
private void testCustomSupportedIncludeAndExcludeFilter(ClassPathScanningCandidateComponentProvider provider) {
provider.addIncludeFilter(new AnnotationTypeFilter(Component.class));
provider.addExcludeFilter(new AnnotationTypeFilter(Service.class));
provider.addExcludeFilter(new AnnotationTypeFilter(Repository.class));
@ -227,7 +221,7 @@ public class ClassPathScanningCandidateComponentProviderTests { @@ -227,7 +221,7 @@ public class ClassPathScanningCandidateComponentProviderTests {
assertThat(containsBeanClass(candidates, ServiceInvocationCounter.class)).isTrue();
assertThat(containsBeanClass(candidates, BarComponent.class)).isTrue();
assertThat(candidates.size()).isEqualTo(3);
assertBeanDefinitionType(candidates, expectedBeanDefinitionType);
assertBeanDefinitionType(candidates);
}
@Test
@ -240,7 +234,7 @@ public class ClassPathScanningCandidateComponentProviderTests { @@ -240,7 +234,7 @@ public class ClassPathScanningCandidateComponentProviderTests {
Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_BASE_PACKAGE);
assertThat(containsBeanClass(candidates, DefaultNamedComponent.class)).isTrue();
assertThat(candidates.size()).isEqualTo(1);
assertBeanDefinitionType(candidates, ScannedGenericBeanDefinition.class);
assertBeanDefinitionType(candidates);
}
@Test
@ -251,7 +245,7 @@ public class ClassPathScanningCandidateComponentProviderTests { @@ -251,7 +245,7 @@ public class ClassPathScanningCandidateComponentProviderTests {
Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_BASE_PACKAGE);
assertThat(containsBeanClass(candidates, StubFooDao.class)).isTrue();
assertThat(candidates.size()).isEqualTo(1);
assertBeanDefinitionType(candidates, ScannedGenericBeanDefinition.class);
assertBeanDefinitionType(candidates);
}
@Test
@ -260,7 +254,7 @@ public class ClassPathScanningCandidateComponentProviderTests { @@ -260,7 +254,7 @@ public class ClassPathScanningCandidateComponentProviderTests {
provider.setResourceLoader(new DefaultResourceLoader(
CandidateComponentsTestClassLoader.disableIndex(getClass().getClassLoader())));
provider.addExcludeFilter(new RegexPatternTypeFilter(Pattern.compile(TEST_BASE_PACKAGE + ".*Named.*")));
testExclude(provider, ScannedGenericBeanDefinition.class);
testExclude(provider);
}
@Test
@ -268,18 +262,17 @@ public class ClassPathScanningCandidateComponentProviderTests { @@ -268,18 +262,17 @@ public class ClassPathScanningCandidateComponentProviderTests {
ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true);
provider.setResourceLoader(new DefaultResourceLoader(TEST_BASE_CLASSLOADER));
provider.addExcludeFilter(new RegexPatternTypeFilter(Pattern.compile(TEST_BASE_PACKAGE + ".*Named.*")));
testExclude(provider, AnnotatedGenericBeanDefinition.class);
testExclude(provider);
}
private void testExclude(ClassPathScanningCandidateComponentProvider provider,
Class<? extends BeanDefinition> expectedBeanDefinitionType) {
private void testExclude(ClassPathScanningCandidateComponentProvider provider) {
Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_BASE_PACKAGE);
assertThat(containsBeanClass(candidates, FooServiceImpl.class)).isTrue();
assertThat(containsBeanClass(candidates, StubFooDao.class)).isTrue();
assertThat(containsBeanClass(candidates, ServiceInvocationCounter.class)).isTrue();
assertThat(containsBeanClass(candidates, BarComponent.class)).isTrue();
assertThat(candidates.size()).isEqualTo(4);
assertBeanDefinitionType(candidates, expectedBeanDefinitionType);
assertBeanDefinitionType(candidates);
}
@Test
@ -519,10 +512,9 @@ public class ClassPathScanningCandidateComponentProviderTests { @@ -519,10 +512,9 @@ public class ClassPathScanningCandidateComponentProviderTests {
return false;
}
private void assertBeanDefinitionType(Set<BeanDefinition> candidates,
Class<? extends BeanDefinition> expectedType) {
private void assertBeanDefinitionType(Set<BeanDefinition> candidates) {
candidates.forEach(c ->
assertThat(c).isInstanceOf(expectedType)
assertThat(c).isInstanceOf(ScannedGenericBeanDefinition.class)
);
}

Loading…
Cancel
Save