Skip to content

Commit bca5a36

Browse files
committed
Explicit error message for bean name clash with containing configuration class
Issue: SPR-15775
1 parent 12114a9 commit bca5a36

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,11 @@ private void loadBeanDefinitionsForBeanMethod(BeanMethod beanMethod) {
198198

199199
// Has this effectively been overridden before (e.g. via XML)?
200200
if (isOverriddenByExistingDefinition(beanMethod, beanName)) {
201+
if (beanName.equals(beanMethod.getConfigurationClass().getBeanName())) {
202+
throw new BeanDefinitionStoreException(beanMethod.getConfigurationClass().getResource().getDescription(),
203+
beanName, "Bean name derived from @Bean method '" + beanMethod.getMetadata().getMethodName() +
204+
"' clashes with bean name for containing configuration class; please make those names unique!");
205+
}
201206
return;
202207
}
203208

spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassPostProcessorTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,12 @@ public void testBeanLookupFromSameConfigurationClass() {
800800
assertSame(ctx.getBean(TestBean.class), bean.getTestBean());
801801
}
802802

803+
@Test(expected = BeanDefinitionStoreException.class)
804+
public void testNameClashBetweenConfigurationClassAndBean() {
805+
ApplicationContext ctx = new AnnotationConfigApplicationContext(MyTestBean.class);
806+
ctx.getBean("myTestBean", TestBean.class);
807+
}
808+
803809

804810
// -------------------------------------------------------------------------
805811

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2002-2017 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;
18+
19+
/**
20+
* @author Juergen Hoeller
21+
*/
22+
@Configuration
23+
class MyTestBean {
24+
25+
@Bean
26+
public org.springframework.tests.sample.beans.TestBean myTestBean() {
27+
return new org.springframework.tests.sample.beans.TestBean();
28+
}
29+
30+
}

0 commit comments

Comments
 (0)