Skip to content

Commit c8b4934

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 34b9ca3 commit c8b4934

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()) {
@@ -381,6 +380,7 @@ private static class ConfigurationClassBeanDefinition extends RootBeanDefinition
381380
public ConfigurationClassBeanDefinition(ConfigurationClass configClass, MethodMetadata beanMethodMetadata) {
382381
this.annotationMetadata = configClass.getMetadata();
383382
this.factoryMethodMetadata = beanMethodMetadata;
383+
setResource(configClass.getResource());
384384
setLenientConstructorResolution(false);
385385
}
386386

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-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.
@@ -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

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

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

0 commit comments

Comments
 (0)