Skip to content

Commit 3a15595

Browse files
committed
Introspect originating bean definition as configuration class candidate
Issue: SPR-16756 (cherry picked from commit c8b6233)
1 parent 289a609 commit 3a15595

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.
@@ -121,8 +121,8 @@ public void loadBeanDefinitions(Set<ConfigurationClass> configurationModel) {
121121
* Read a particular {@link ConfigurationClass}, registering bean definitions
122122
* for the class itself and all of its {@link Bean} methods.
123123
*/
124-
private void loadBeanDefinitionsForConfigurationClass(ConfigurationClass configClass,
125-
TrackedConditionEvaluator trackedConditionEvaluator) {
124+
private void loadBeanDefinitionsForConfigurationClass(
125+
ConfigurationClass configClass, TrackedConditionEvaluator trackedConditionEvaluator) {
126126

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

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
@@ -289,9 +289,12 @@ protected final SourceClass doProcessConfigurationClass(ConfigurationClass confi
289289
this.componentScanParser.parse(componentScan, sourceClass.getMetadata().getClassName());
290290
// Check the set of scanned definitions for any further config classes and parse recursively if needed
291291
for (BeanDefinitionHolder holder : scannedBeanDefinitions) {
292-
if (ConfigurationClassUtils.checkConfigurationClassCandidate(
293-
holder.getBeanDefinition(), this.metadataReaderFactory)) {
294-
parse(holder.getBeanDefinition().getBeanClassName(), holder.getBeanName());
292+
BeanDefinition bdCand = holder.getBeanDefinition().getOriginatingBeanDefinition();
293+
if (bdCand == null) {
294+
bdCand = holder.getBeanDefinition();
295+
}
296+
if (ConfigurationClassUtils.checkConfigurationClassCandidate(bdCand, this.metadataReaderFactory)) {
297+
parse(bdCand.getBeanClassName(), holder.getBeanName());
295298
}
296299
}
297300
}

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.
@@ -38,7 +38,7 @@
3838
import org.springframework.stereotype.Component;
3939

4040
/**
41-
* Utilities for processing @{@link Configuration} classes.
41+
* Utilities for identifying @{@link Configuration} classes.
4242
*
4343
* @author Chris Beams
4444
* @author Juergen Hoeller
@@ -59,7 +59,7 @@ abstract class ConfigurationClassUtils {
5959

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

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

6464
static {
6565
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)