Skip to content

Commit 22f421c

Browse files
committed
Introspect originating bean definition as configuration class candidate
Issue: SPR-16756 (cherry picked from commit c8b6233)
1 parent 9dc538a commit 22f421c

File tree

6 files changed

+124
-9
lines changed

6 files changed

+124
-9
lines changed

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

Lines changed: 4 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-2018 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.
@@ -122,8 +122,8 @@ public void loadBeanDefinitions(Set<ConfigurationClass> configurationModel) {
122122
* Read a particular {@link ConfigurationClass}, registering bean definitions
123123
* for the class itself and all of its {@link Bean} methods.
124124
*/
125-
private void loadBeanDefinitionsForConfigurationClass(ConfigurationClass configClass,
126-
TrackedConditionEvaluator trackedConditionEvaluator) {
125+
private void loadBeanDefinitionsForConfigurationClass(
126+
ConfigurationClass configClass, TrackedConditionEvaluator trackedConditionEvaluator) {
127127

128128
if (trackedConditionEvaluator.shouldSkip(configClass)) {
129129
String beanName = configClass.getBeanName();
@@ -140,6 +140,7 @@ private void loadBeanDefinitionsForConfigurationClass(ConfigurationClass configC
140140
for (BeanMethod beanMethod : configClass.getBeanMethods()) {
141141
loadBeanDefinitionsForBeanMethod(beanMethod);
142142
}
143+
143144
loadBeanDefinitionsFromImportedResources(configClass.getImportedResources());
144145
loadBeanDefinitionsFromRegistrars(configClass.getImportBeanDefinitionRegistrars());
145146
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,12 @@ protected final SourceClass doProcessConfigurationClass(ConfigurationClass confi
288288
this.componentScanParser.parse(componentScan, sourceClass.getMetadata().getClassName());
289289
// Check the set of scanned definitions for any further config classes and parse recursively if needed
290290
for (BeanDefinitionHolder holder : scannedBeanDefinitions) {
291-
if (ConfigurationClassUtils.checkConfigurationClassCandidate(
292-
holder.getBeanDefinition(), this.metadataReaderFactory)) {
293-
parse(holder.getBeanDefinition().getBeanClassName(), holder.getBeanName());
291+
BeanDefinition bdCand = holder.getBeanDefinition().getOriginatingBeanDefinition();
292+
if (bdCand == null) {
293+
bdCand = holder.getBeanDefinition();
294+
}
295+
if (ConfigurationClassUtils.checkConfigurationClassCandidate(bdCand, this.metadataReaderFactory)) {
296+
parse(bdCand.getBeanClassName(), holder.getBeanName());
294297
}
295298
}
296299
}

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

Lines changed: 3 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-2018 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.
@@ -39,7 +39,7 @@
3939
import org.springframework.stereotype.Component;
4040

4141
/**
42-
* Utilities for processing @{@link Configuration} classes.
42+
* Utilities for identifying @{@link Configuration} classes.
4343
*
4444
* @author Chris Beams
4545
* @author Juergen Hoeller
@@ -60,7 +60,7 @@ abstract class ConfigurationClassUtils {
6060

6161
private static final Log logger = LogFactory.getLog(ConfigurationClassUtils.class);
6262

63-
private static final Set<String> candidateIndicators = new HashSet<>(4);
63+
private static final Set<String> candidateIndicators = new HashSet<>(8);
6464

6565
static {
6666
candidateIndicators.add(Component.class.getName());
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2002-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.context.annotation.spr16756;
18+
19+
import org.springframework.beans.factory.annotation.Autowired;
20+
import org.springframework.context.annotation.Scope;
21+
import org.springframework.context.annotation.ScopedProxyMode;
22+
import org.springframework.stereotype.Component;
23+
24+
@Component
25+
public class ScannedComponent {
26+
27+
@Autowired
28+
private State state;
29+
30+
public String iDoAnything() {
31+
return state.anyMethod();
32+
}
33+
34+
35+
public interface State {
36+
37+
String anyMethod();
38+
}
39+
40+
41+
@Component
42+
@Scope(proxyMode = ScopedProxyMode.INTERFACES, value = "prototype")
43+
public static class StateImpl implements State {
44+
45+
public String anyMethod() {
46+
return "anyMethod called";
47+
}
48+
}
49+
50+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2002-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.context.annotation.spr16756;
18+
19+
import org.springframework.context.annotation.ComponentScan;
20+
21+
@ComponentScan
22+
public class ScanningConfiguration {
23+
24+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2002-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.context.annotation.spr16756;
18+
19+
import org.junit.Test;
20+
21+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
22+
23+
/**
24+
* @author Juergen Hoeller
25+
*/
26+
public class Spr16756Tests {
27+
28+
@Test
29+
public void shouldNotFailOnNestedScopedComponent() {
30+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
31+
context.register(ScanningConfiguration.class);
32+
context.refresh();
33+
context.getBean(ScannedComponent.class);
34+
context.getBean(ScannedComponent.State.class);
35+
}
36+
37+
}

0 commit comments

Comments
 (0)