Skip to content

Commit d0fc38e

Browse files
committed
Fixed inner bean name determination for multi-level nesting scenario, calculating a unique bean name as early as possible now (and for any kind of bean scope)
Our per-bean caching in AutowiredAnnotationBeanPostProcessor and co relies on unique bean names, so this change fixes potential cache mismatch problems occuring there. Issue: SPR-11131 (cherry picked from commit 242ecdc)
1 parent 8e52e65 commit d0fc38e

File tree

2 files changed

+9
-19
lines changed

2 files changed

+9
-19
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/support/BeanDefinitionValueResolver.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,8 @@ private Object resolveInnerBean(Object argName, String innerBeanName, BeanDefini
256256
mbd = this.beanFactory.getMergedBeanDefinition(innerBeanName, innerBd, this.beanDefinition);
257257
// Check given bean name whether it is unique. If not already unique,
258258
// add counter - increasing the counter until the name is unique.
259-
String actualInnerBeanName = innerBeanName;
260-
if (mbd.isSingleton()) {
261-
actualInnerBeanName = adaptInnerBeanName(innerBeanName);
262-
}
259+
String actualInnerBeanName = adaptInnerBeanName(innerBeanName);
260+
this.beanFactory.registerContainedBean(actualInnerBeanName, this.beanName);
263261
// Guarantee initialization of beans that the inner bean depends on.
264262
String[] dependsOn = mbd.getDependsOn();
265263
if (dependsOn != null) {
@@ -269,7 +267,6 @@ private Object resolveInnerBean(Object argName, String innerBeanName, BeanDefini
269267
}
270268
}
271269
Object innerBean = this.beanFactory.createBean(actualInnerBeanName, mbd, null);
272-
this.beanFactory.registerContainedBean(actualInnerBeanName, this.beanName);
273270
if (innerBean instanceof FactoryBean) {
274271
boolean synthetic = mbd.isSynthetic();
275272
return this.beanFactory.getObjectFromFactoryBean((FactoryBean) innerBean, actualInnerBeanName, !synthetic);

spring-context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,6 @@
1616

1717
package org.springframework.beans.factory.xml;
1818

19-
import static org.hamcrest.CoreMatchers.instanceOf;
20-
import static org.junit.Assert.assertEquals;
21-
import static org.junit.Assert.assertFalse;
22-
import static org.junit.Assert.assertNotNull;
23-
import static org.junit.Assert.assertNotSame;
24-
import static org.junit.Assert.assertNull;
25-
import static org.junit.Assert.assertSame;
26-
import static org.junit.Assert.assertThat;
27-
import static org.junit.Assert.assertTrue;
28-
import static org.junit.Assert.fail;
29-
3019
import java.io.File;
3120
import java.io.IOException;
3221
import java.io.InputStream;
@@ -39,6 +28,8 @@
3928

4029
import org.apache.commons.logging.LogFactory;
4130
import org.junit.Test;
31+
import org.xml.sax.InputSource;
32+
4233
import org.springframework.aop.framework.ProxyFactory;
4334
import org.springframework.aop.support.AopUtils;
4435
import org.springframework.beans.BeansException;
@@ -73,7 +64,9 @@
7364
import org.springframework.util.FileCopyUtils;
7465
import org.springframework.util.SerializationTestUtils;
7566
import org.springframework.util.StopWatch;
76-
import org.xml.sax.InputSource;
67+
68+
import static org.hamcrest.CoreMatchers.*;
69+
import static org.junit.Assert.*;
7770

7871
/**
7972
* Miscellaneous tests for XML bean definitions.
@@ -256,7 +249,7 @@ public void testInnerBeansWithoutDestroy() {
256249
assertEquals(5, hasInnerBeans.getAge());
257250
TestBean inner1 = (TestBean) hasInnerBeans.getSpouse();
258251
assertNotNull(inner1);
259-
assertEquals("innerBean", inner1.getBeanName());
252+
assertTrue(inner1.getBeanName().startsWith("innerBean"));
260253
assertEquals("inner1", inner1.getName());
261254
assertEquals(6, inner1.getAge());
262255

@@ -271,7 +264,7 @@ public void testInnerBeansWithoutDestroy() {
271264
TestBean innerFactory = (TestBean) friends[1];
272265
assertEquals(DummyFactory.SINGLETON_NAME, innerFactory.getName());
273266
TestBean inner5 = (TestBean) friends[2];
274-
assertEquals("innerBean", inner5.getBeanName());
267+
assertTrue(inner5.getBeanName().startsWith("innerBean"));
275268
}
276269

277270
@Test

0 commit comments

Comments
 (0)