Skip to content

Commit 4c59d85

Browse files
committed
Be defensive adding additional imported configs
Refine the logic introduced in commit 71c6eb2 so that additional imported @configuration classes are not considered as candidates if they have already been parsed. Issue: SPR-12233
1 parent 983b533 commit 4c59d85

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

Diff for: spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -322,10 +322,15 @@ else if (ConfigurationClassUtils.checkConfigurationClassCandidate(beanDef, this.
322322
if (registry.getBeanDefinitionCount() > candidateNames.length) {
323323
String[] newCandidateNames = registry.getBeanDefinitionNames();
324324
Set<String> oldCandidateNames = new HashSet<String>(Arrays.asList(candidateNames));
325+
Set<String> alreadyParsedClasses = new HashSet<String>();
326+
for (ConfigurationClass configurationClass : alreadyParsed) {
327+
alreadyParsedClasses.add(configurationClass.getMetadata().getClassName());
328+
}
325329
for (String candidateName : newCandidateNames) {
326330
if (!oldCandidateNames.contains(candidateName)) {
327331
BeanDefinition beanDef = registry.getBeanDefinition(candidateName);
328-
if (ConfigurationClassUtils.checkConfigurationClassCandidate(beanDef, this.metadataReaderFactory)) {
332+
if (ConfigurationClassUtils.checkConfigurationClassCandidate(beanDef, this.metadataReaderFactory)
333+
&& !alreadyParsedClasses.contains(beanDef.getBeanClassName())) {
329334
configCandidates.add(new BeanDefinitionHolder(beanDef, candidateName));
330335
}
331336
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2002-2014 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.spr12233;
18+
19+
import org.junit.Test;
20+
import org.springframework.beans.factory.annotation.Value;
21+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
22+
import org.springframework.context.annotation.ComponentScan;
23+
import org.springframework.context.annotation.ConditionContext;
24+
import org.springframework.context.annotation.Conditional;
25+
import org.springframework.context.annotation.Configuration;
26+
import org.springframework.context.annotation.ConfigurationCondition;
27+
import org.springframework.context.annotation.Import;
28+
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
29+
import org.springframework.core.type.AnnotatedTypeMetadata;
30+
31+
32+
/**
33+
* Tests cornering the regression reported in SPR-12233.
34+
*
35+
* @author Phillip Webb
36+
*/
37+
public class Spr12233Tests {
38+
39+
@Test
40+
public void spr12233() throws Exception {
41+
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
42+
ctx.register(PropertySourcesPlaceholderConfigurer.class);
43+
ctx.register(ImportConfiguration.class);
44+
ctx.refresh();
45+
ctx.close();
46+
}
47+
48+
static class NeverConfigurationCondition implements ConfigurationCondition {
49+
@Override
50+
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
51+
return false;
52+
}
53+
54+
@Override
55+
public ConfigurationPhase getConfigurationPhase() {
56+
return ConfigurationPhase.REGISTER_BEAN;
57+
}
58+
}
59+
60+
@Import(ComponentScanningConfiguration.class)
61+
static class ImportConfiguration {
62+
63+
}
64+
65+
@Configuration
66+
@ComponentScan
67+
static class ComponentScanningConfiguration {
68+
69+
}
70+
71+
72+
@Configuration
73+
@Conditional(NeverConfigurationCondition.class)
74+
static class ConditionWithPropertyValueInjection {
75+
76+
@Value("${idontexist}")
77+
private String property;
78+
}
79+
}

0 commit comments

Comments
 (0)