Skip to content

Commit

Permalink
Create ScannedGenericBeanDefinitions for index-derived components as …
Browse files Browse the repository at this point in the history
…well

Closes gh-24638
  • Loading branch information
jhoeller committed Mar 13, 2020
1 parent 5eb8430 commit 1a81aed
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -29,7 +29,6 @@

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;
Expand Down Expand Up @@ -386,8 +385,7 @@ private Set<BeanDefinition> addCandidateComponentsFromIndex(CandidateComponentsI
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import org.aspectj.lang.annotation.Aspect;
import org.junit.Test;

import org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.index.CandidateComponentsTestClassLoader;
import org.springframework.core.env.ConfigurableEnvironment;
Expand Down Expand Up @@ -80,18 +79,17 @@ public void defaultsWithScan() {
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);
assertTrue(containsBeanClass(candidates, DefaultNamedComponent.class));
assertTrue(containsBeanClass(candidates, NamedComponent.class));
Expand All @@ -101,30 +99,29 @@ private void testDefault(ClassPathScanningCandidateComponentProvider provider,
assertTrue(containsBeanClass(candidates, ServiceInvocationCounter.class));
assertTrue(containsBeanClass(candidates, BarComponent.class));
assertEquals(7, candidates.size());
assertBeanDefinitionType(candidates, expectedBeanDefinitionType);
assertBeanDefinitionType(candidates);
}

@Test
public void antStylePackageWithScan() {
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");
assertTrue(containsBeanClass(candidates, BarComponent.class));
assertEquals(1, candidates.size());
assertBeanDefinitionType(candidates, expectedBeanDefinitionType);
assertBeanDefinitionType(candidates);
}

@Test
Expand All @@ -151,74 +148,71 @@ public void customFiltersFollowedByResetUseIndex() {
provider.addIncludeFilter(new AnnotationTypeFilter(Component.class));
provider.resetFilters(true);
Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_BASE_PACKAGE);
assertBeanDefinitionType(candidates, AnnotatedGenericBeanDefinition.class);
assertBeanDefinitionType(candidates);
}

@Test
public void customAnnotationTypeIncludeFilterWithScan() {
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
public void customAssignableTypeIncludeFilterWithScan() {
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.
assertTrue(containsBeanClass(candidates, AutowiredQualifierFooService.class));
assertTrue(containsBeanClass(candidates, FooServiceImpl.class));
assertTrue(containsBeanClass(candidates, ScopedProxyTestBean.class));
assertEquals(3, candidates.size());
assertBeanDefinitionType(candidates, expectedBeanDefinitionType);
assertBeanDefinitionType(candidates);
}

@Test
public void customSupportedIncludeAndExcludedFilterWithScan() {
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));
Expand All @@ -227,7 +221,7 @@ private void testCustomSupportedIncludeAndExcludeFilter(ClassPathScanningCandida
assertTrue(containsBeanClass(candidates, ServiceInvocationCounter.class));
assertTrue(containsBeanClass(candidates, BarComponent.class));
assertEquals(3, candidates.size());
assertBeanDefinitionType(candidates, expectedBeanDefinitionType);
assertBeanDefinitionType(candidates);
}

@Test
Expand All @@ -240,7 +234,7 @@ public void customSupportIncludeFilterWithNonIndexedTypeUseScan() {
Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_BASE_PACKAGE);
assertTrue(containsBeanClass(candidates, DefaultNamedComponent.class));
assertEquals(1, candidates.size());
assertBeanDefinitionType(candidates, ScannedGenericBeanDefinition.class);
assertBeanDefinitionType(candidates);
}

@Test
Expand All @@ -251,7 +245,7 @@ public void customNotSupportedIncludeFilterUseScan() {
Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_BASE_PACKAGE);
assertTrue(containsBeanClass(candidates, StubFooDao.class));
assertEquals(1, candidates.size());
assertBeanDefinitionType(candidates, ScannedGenericBeanDefinition.class);
assertBeanDefinitionType(candidates);
}

@Test
Expand All @@ -260,26 +254,25 @@ public void excludeFilterWithScan() {
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
public void excludeFilterWithIndex() {
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);
assertTrue(containsBeanClass(candidates, FooServiceImpl.class));
assertTrue(containsBeanClass(candidates, StubFooDao.class));
assertTrue(containsBeanClass(candidates, ServiceInvocationCounter.class));
assertTrue(containsBeanClass(candidates, BarComponent.class));
assertEquals(4, candidates.size());
assertBeanDefinitionType(candidates, expectedBeanDefinitionType);
assertBeanDefinitionType(candidates);
}

@Test
Expand Down Expand Up @@ -510,10 +503,9 @@ private boolean containsBeanClass(Set<BeanDefinition> candidates, Class<?> beanC
return false;
}

private void assertBeanDefinitionType(Set<BeanDefinition> candidates,
Class<? extends BeanDefinition> expectedType) {
private void assertBeanDefinitionType(Set<BeanDefinition> candidates) {
candidates.forEach(c -> {
assertThat(c, is(instanceOf(expectedType)));
assertThat(c, is(instanceOf(ScannedGenericBeanDefinition.class)));
});
}

Expand Down

0 comments on commit 1a81aed

Please sign in to comment.