Skip to content

Commit 6dcabd8

Browse files
committed
Store source in index-derived ScannedGenericBeanDefinition as well
Includes consistent constructor-level storage of derived resource in ScannedGenericBeanDefinition and ConfigurationClassBeanDefinition. See gh-24978
1 parent 6a6ad05 commit 6dcabd8

File tree

4 files changed

+66
-7
lines changed

4 files changed

+66
-7
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ private Set<BeanDefinition> addCandidateComponentsFromIndex(CandidateComponentsI
386386
MetadataReader metadataReader = getMetadataReaderFactory().getMetadataReader(type);
387387
if (isCandidateComponent(metadataReader)) {
388388
ScannedGenericBeanDefinition sbd = new ScannedGenericBeanDefinition(metadataReader);
389+
sbd.setSource(metadataReader.getResource());
389390
if (isCandidateComponent(sbd)) {
390391
if (debugEnabled) {
391392
logger.debug("Using candidate component class from index: " + type);
@@ -428,7 +429,6 @@ private Set<BeanDefinition> scanCandidateComponents(String basePackage) {
428429
MetadataReader metadataReader = getMetadataReaderFactory().getMetadataReader(resource);
429430
if (isCandidateComponent(metadataReader)) {
430431
ScannedGenericBeanDefinition sbd = new ScannedGenericBeanDefinition(metadataReader);
431-
sbd.setResource(resource);
432432
sbd.setSource(resource);
433433
if (isCandidateComponent(sbd)) {
434434
if (debugEnabled) {

spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassBeanDefinitionReader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -209,7 +209,6 @@ private void loadBeanDefinitionsForBeanMethod(BeanMethod beanMethod) {
209209
}
210210

211211
ConfigurationClassBeanDefinition beanDef = new ConfigurationClassBeanDefinition(configClass, metadata);
212-
beanDef.setResource(configClass.getResource());
213212
beanDef.setSource(this.sourceExtractor.extractSource(metadata, configClass.getResource()));
214213

215214
if (metadata.isStatic()) {
@@ -375,6 +374,7 @@ private static class ConfigurationClassBeanDefinition extends RootBeanDefinition
375374
public ConfigurationClassBeanDefinition(ConfigurationClass configClass, MethodMetadata beanMethodMetadata) {
376375
this.annotationMetadata = configClass.getMetadata();
377376
this.factoryMethodMetadata = beanMethodMetadata;
377+
setResource(configClass.getResource());
378378
setLenientConstructorResolution(false);
379379
}
380380

spring-context/src/main/java/org/springframework/context/annotation/ScannedGenericBeanDefinition.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -60,6 +60,7 @@ public ScannedGenericBeanDefinition(MetadataReader metadataReader) {
6060
Assert.notNull(metadataReader, "MetadataReader must not be null");
6161
this.metadata = metadataReader.getAnnotationMetadata();
6262
setBeanClassName(this.metadata.getClassName());
63+
setResource(metadataReader.getResource());
6364
}
6465

6566

spring-context/src/test/java/org/springframework/context/annotation/ClassPathBeanDefinitionScannerTests.java

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,16 +28,18 @@
2828
import org.springframework.beans.factory.BeanCreationException;
2929
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
3030
import org.springframework.beans.factory.config.BeanDefinition;
31+
import org.springframework.beans.factory.support.AbstractBeanDefinition;
3132
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
3233
import org.springframework.beans.factory.support.RootBeanDefinition;
3334
import org.springframework.beans.factory.support.StaticListableBeanFactory;
3435
import org.springframework.context.MessageSource;
3536
import org.springframework.context.annotation2.NamedStubDao2;
37+
import org.springframework.context.index.CandidateComponentsTestClassLoader;
3638
import org.springframework.context.support.GenericApplicationContext;
39+
import org.springframework.core.io.ClassPathResource;
3740
import org.springframework.core.type.filter.AnnotationTypeFilter;
3841
import org.springframework.core.type.filter.AssignableTypeFilter;
3942
import org.springframework.stereotype.Component;
40-
import org.springframework.tests.sample.beans.TestBean;
4143

4244
import static org.junit.Assert.*;
4345

@@ -103,10 +105,66 @@ public void testSimpleScanWithDefaultFiltersAndPrimaryLazyBean() {
103105
@Test
104106
public void testDoubleScan() {
105107
GenericApplicationContext context = new GenericApplicationContext();
108+
106109
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context);
107110
int beanCount = scanner.scan(BASE_PACKAGE);
108111
assertEquals(13, beanCount);
109-
scanner.scan(BASE_PACKAGE);
112+
113+
ClassPathBeanDefinitionScanner scanner2 = new ClassPathBeanDefinitionScanner(context) {
114+
@Override
115+
protected void postProcessBeanDefinition(AbstractBeanDefinition beanDefinition, String beanName) {
116+
super.postProcessBeanDefinition(beanDefinition, beanName);
117+
beanDefinition.setAttribute("someDifference", "someValue");
118+
}
119+
};
120+
scanner2.scan(BASE_PACKAGE);
121+
122+
assertTrue(context.containsBean("serviceInvocationCounter"));
123+
assertTrue(context.containsBean("fooServiceImpl"));
124+
assertTrue(context.containsBean("stubFooDao"));
125+
assertTrue(context.containsBean("myNamedComponent"));
126+
assertTrue(context.containsBean("myNamedDao"));
127+
assertTrue(context.containsBean("thoreau"));
128+
}
129+
130+
@Test
131+
public void testWithIndex() {
132+
GenericApplicationContext context = new GenericApplicationContext();
133+
context.setClassLoader(CandidateComponentsTestClassLoader.index(
134+
ClassPathScanningCandidateComponentProviderTests.class.getClassLoader(),
135+
new ClassPathResource("spring.components", FooServiceImpl.class)));
136+
137+
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context);
138+
int beanCount = scanner.scan(BASE_PACKAGE);
139+
assertEquals(13, beanCount);
140+
141+
assertTrue(context.containsBean("serviceInvocationCounter"));
142+
assertTrue(context.containsBean("fooServiceImpl"));
143+
assertTrue(context.containsBean("stubFooDao"));
144+
assertTrue(context.containsBean("myNamedComponent"));
145+
assertTrue(context.containsBean("myNamedDao"));
146+
assertTrue(context.containsBean("thoreau"));
147+
}
148+
149+
@Test
150+
public void testDoubleScanWithIndex() {
151+
GenericApplicationContext context = new GenericApplicationContext();
152+
context.setClassLoader(CandidateComponentsTestClassLoader.index(
153+
ClassPathScanningCandidateComponentProviderTests.class.getClassLoader(),
154+
new ClassPathResource("spring.components", FooServiceImpl.class)));
155+
156+
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context);
157+
int beanCount = scanner.scan(BASE_PACKAGE);
158+
assertEquals(13, beanCount);
159+
160+
ClassPathBeanDefinitionScanner scanner2 = new ClassPathBeanDefinitionScanner(context) {
161+
@Override
162+
protected void postProcessBeanDefinition(AbstractBeanDefinition beanDefinition, String beanName) {
163+
super.postProcessBeanDefinition(beanDefinition, beanName);
164+
beanDefinition.setAttribute("someDifference", "someValue");
165+
}
166+
};
167+
scanner2.scan(BASE_PACKAGE);
110168

111169
assertTrue(context.containsBean("serviceInvocationCounter"));
112170
assertTrue(context.containsBean("fooServiceImpl"));

0 commit comments

Comments
 (0)