Skip to content

Commit 127e879

Browse files
committed
Store source in index-derived ScannedGenericBeanDefinition as well
Includes consistent constructor-level storage of derived resource in ScannedGenericBeanDefinition and ConfigurationClassBeanDefinition. Closes gh-24978
1 parent 59ecd49 commit 127e879

File tree

4 files changed

+66
-6
lines changed

4 files changed

+66
-6
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-2019 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.
@@ -212,7 +212,6 @@ private void loadBeanDefinitionsForBeanMethod(BeanMethod beanMethod) {
212212
}
213213

214214
ConfigurationClassBeanDefinition beanDef = new ConfigurationClassBeanDefinition(configClass, metadata);
215-
beanDef.setResource(configClass.getResource());
216215
beanDef.setSource(this.sourceExtractor.extractSource(metadata, configClass.getResource()));
217216

218217
if (metadata.isStatic()) {
@@ -402,6 +401,7 @@ private static class ConfigurationClassBeanDefinition extends RootBeanDefinition
402401
public ConfigurationClassBeanDefinition(ConfigurationClass configClass, MethodMetadata beanMethodMetadata) {
403402
this.annotationMetadata = configClass.getMetadata();
404403
this.factoryMethodMetadata = beanMethodMetadata;
404+
setResource(configClass.getResource());
405405
setLenientConstructorResolution(false);
406406
}
407407

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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 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,13 +28,16 @@
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.beans.testfixture.beans.TestBean;
3536
import org.springframework.context.MessageSource;
3637
import org.springframework.context.annotation2.NamedStubDao2;
3738
import org.springframework.context.support.GenericApplicationContext;
39+
import org.springframework.context.testfixture.index.CandidateComponentsTestClassLoader;
40+
import org.springframework.core.io.ClassPathResource;
3841
import org.springframework.core.type.filter.AnnotationTypeFilter;
3942
import org.springframework.core.type.filter.AssignableTypeFilter;
4043
import org.springframework.stereotype.Component;
@@ -104,10 +107,66 @@ public void testSimpleScanWithDefaultFiltersAndPrimaryLazyBean() {
104107
@Test
105108
public void testDoubleScan() {
106109
GenericApplicationContext context = new GenericApplicationContext();
110+
107111
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context);
108112
int beanCount = scanner.scan(BASE_PACKAGE);
109113
assertThat(beanCount).isEqualTo(12);
110-
scanner.scan(BASE_PACKAGE);
114+
115+
ClassPathBeanDefinitionScanner scanner2 = new ClassPathBeanDefinitionScanner(context) {
116+
@Override
117+
protected void postProcessBeanDefinition(AbstractBeanDefinition beanDefinition, String beanName) {
118+
super.postProcessBeanDefinition(beanDefinition, beanName);
119+
beanDefinition.setAttribute("someDifference", "someValue");
120+
}
121+
};
122+
scanner2.scan(BASE_PACKAGE);
123+
124+
assertThat(context.containsBean("serviceInvocationCounter")).isTrue();
125+
assertThat(context.containsBean("fooServiceImpl")).isTrue();
126+
assertThat(context.containsBean("stubFooDao")).isTrue();
127+
assertThat(context.containsBean("myNamedComponent")).isTrue();
128+
assertThat(context.containsBean("myNamedDao")).isTrue();
129+
assertThat(context.containsBean("thoreau")).isTrue();
130+
}
131+
132+
@Test
133+
public void testWithIndex() {
134+
GenericApplicationContext context = new GenericApplicationContext();
135+
context.setClassLoader(CandidateComponentsTestClassLoader.index(
136+
ClassPathScanningCandidateComponentProviderTests.class.getClassLoader(),
137+
new ClassPathResource("spring.components", FooServiceImpl.class)));
138+
139+
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context);
140+
int beanCount = scanner.scan(BASE_PACKAGE);
141+
assertThat(beanCount).isEqualTo(12);
142+
143+
assertThat(context.containsBean("serviceInvocationCounter")).isTrue();
144+
assertThat(context.containsBean("fooServiceImpl")).isTrue();
145+
assertThat(context.containsBean("stubFooDao")).isTrue();
146+
assertThat(context.containsBean("myNamedComponent")).isTrue();
147+
assertThat(context.containsBean("myNamedDao")).isTrue();
148+
assertThat(context.containsBean("thoreau")).isTrue();
149+
}
150+
151+
@Test
152+
public void testDoubleScanWithIndex() {
153+
GenericApplicationContext context = new GenericApplicationContext();
154+
context.setClassLoader(CandidateComponentsTestClassLoader.index(
155+
ClassPathScanningCandidateComponentProviderTests.class.getClassLoader(),
156+
new ClassPathResource("spring.components", FooServiceImpl.class)));
157+
158+
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context);
159+
int beanCount = scanner.scan(BASE_PACKAGE);
160+
assertThat(beanCount).isEqualTo(12);
161+
162+
ClassPathBeanDefinitionScanner scanner2 = new ClassPathBeanDefinitionScanner(context) {
163+
@Override
164+
protected void postProcessBeanDefinition(AbstractBeanDefinition beanDefinition, String beanName) {
165+
super.postProcessBeanDefinition(beanDefinition, beanName);
166+
beanDefinition.setAttribute("someDifference", "someValue");
167+
}
168+
};
169+
scanner2.scan(BASE_PACKAGE);
111170

112171
assertThat(context.containsBean("serviceInvocationCounter")).isTrue();
113172
assertThat(context.containsBean("fooServiceImpl")).isTrue();

0 commit comments

Comments
 (0)