From bee63e38c176bce9acaf4774d2fd47289946f7b8 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 11 Mar 2020 14:54:16 +0100 Subject: [PATCH] Create ScannedGenericBeanDefinitions for index-derived components as well Closes gh-24638 --- ...athScanningCandidateComponentProvider.java | 6 +- ...anningCandidateComponentProviderTests.java | 66 ++++++++----------- 2 files changed, 31 insertions(+), 41 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ClassPathScanningCandidateComponentProvider.java b/spring-context/src/main/java/org/springframework/context/annotation/ClassPathScanningCandidateComponentProvider.java index 7ccc8d5544a..47137031b55 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ClassPathScanningCandidateComponentProvider.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ClassPathScanningCandidateComponentProvider.java @@ -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; 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 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); diff --git a/spring-context/src/test/java/org/springframework/context/annotation/ClassPathScanningCandidateComponentProviderTests.java b/spring-context/src/test/java/org/springframework/context/annotation/ClassPathScanningCandidateComponentProviderTests.java index f4809a51d59..bb662105c3a 100644 --- a/spring-context/src/test/java/org/springframework/context/annotation/ClassPathScanningCandidateComponentProviderTests.java +++ b/spring-context/src/test/java/org/springframework/context/annotation/ClassPathScanningCandidateComponentProviderTests.java @@ -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 { 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 expectedBeanDefinitionType) { + private void testDefault(ClassPathScanningCandidateComponentProvider provider) { Set 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 { 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 { 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 expectedBeanDefinitionType) { + private void testAntStyle(ClassPathScanningCandidateComponentProvider provider) { Set 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 { provider.addIncludeFilter(new AnnotationTypeFilter(Component.class)); provider.resetFilters(true); Set candidates = provider.findCandidateComponents(TEST_BASE_PACKAGE); - assertBeanDefinitionType(candidates, AnnotatedGenericBeanDefinition.class); + assertBeanDefinitionType(candidates); } @Test @@ -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 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 { 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 expectedBeanDefinitionType) { + private void testCustomAssignableTypeIncludeFilter(ClassPathScanningCandidateComponentProvider provider) { provider.addIncludeFilter(new AssignableTypeFilter(FooService.class)); Set candidates = provider.findCandidateComponents(TEST_BASE_PACKAGE); // Interfaces/Abstract class are filtered out automatically. @@ -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 { 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 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 { 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 { Set 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 { Set 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 { 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 { 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 expectedBeanDefinitionType) { + private void testExclude(ClassPathScanningCandidateComponentProvider provider) { Set 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 { return false; } - private void assertBeanDefinitionType(Set candidates, - Class expectedType) { + private void assertBeanDefinitionType(Set candidates) { candidates.forEach(c -> - assertThat(c).isInstanceOf(expectedType) + assertThat(c).isInstanceOf(ScannedGenericBeanDefinition.class) ); }