diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/BeanDefinitionValueResolver.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/BeanDefinitionValueResolver.java index 9640067784..3301695a7e 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/BeanDefinitionValueResolver.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/BeanDefinitionValueResolver.java @@ -256,10 +256,8 @@ class BeanDefinitionValueResolver { mbd = this.beanFactory.getMergedBeanDefinition(innerBeanName, innerBd, this.beanDefinition); // Check given bean name whether it is unique. If not already unique, // add counter - increasing the counter until the name is unique. - String actualInnerBeanName = innerBeanName; - if (mbd.isSingleton()) { - actualInnerBeanName = adaptInnerBeanName(innerBeanName); - } + String actualInnerBeanName = adaptInnerBeanName(innerBeanName); + this.beanFactory.registerContainedBean(actualInnerBeanName, this.beanName); // Guarantee initialization of beans that the inner bean depends on. String[] dependsOn = mbd.getDependsOn(); if (dependsOn != null) { @@ -269,7 +267,6 @@ class BeanDefinitionValueResolver { } } Object innerBean = this.beanFactory.createBean(actualInnerBeanName, mbd, null); - this.beanFactory.registerContainedBean(actualInnerBeanName, this.beanName); if (innerBean instanceof FactoryBean) { boolean synthetic = mbd.isSynthetic(); return this.beanFactory.getObjectFromFactoryBean((FactoryBean) innerBean, actualInnerBeanName, !synthetic); diff --git a/spring-context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests.java b/spring-context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests.java index 6115b4979e..2f30a69459 100644 --- a/spring-context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests.java +++ b/spring-context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests.java @@ -16,17 +16,6 @@ package org.springframework.beans.factory.xml; -import static org.hamcrest.CoreMatchers.instanceOf; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNotSame; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -39,6 +28,8 @@ import java.util.Map; import org.apache.commons.logging.LogFactory; import org.junit.Test; +import org.xml.sax.InputSource; + import org.springframework.aop.framework.ProxyFactory; import org.springframework.aop.support.AopUtils; import org.springframework.beans.BeansException; @@ -73,7 +64,9 @@ import org.springframework.tests.sample.beans.factory.DummyFactory; import org.springframework.util.FileCopyUtils; import org.springframework.util.SerializationTestUtils; import org.springframework.util.StopWatch; -import org.xml.sax.InputSource; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; /** * Miscellaneous tests for XML bean definitions. @@ -256,7 +249,7 @@ public final class XmlBeanFactoryTests { assertEquals(5, hasInnerBeans.getAge()); TestBean inner1 = (TestBean) hasInnerBeans.getSpouse(); assertNotNull(inner1); - assertEquals("innerBean", inner1.getBeanName()); + assertTrue(inner1.getBeanName().startsWith("innerBean")); assertEquals("inner1", inner1.getName()); assertEquals(6, inner1.getAge()); @@ -271,7 +264,7 @@ public final class XmlBeanFactoryTests { TestBean innerFactory = (TestBean) friends[1]; assertEquals(DummyFactory.SINGLETON_NAME, innerFactory.getName()); TestBean inner5 = (TestBean) friends[2]; - assertEquals("innerBean", inner5.getBeanName()); + assertTrue(inner5.getBeanName().startsWith("innerBean")); } @Test