child bean definition's scope attribute can be inherited from parent bean definition now (SPR-3542)

This commit is contained in:
Juergen Hoeller
2009-11-12 00:09:05 +00:00
parent 4a25e2dde0
commit d0b6891275
6 changed files with 17 additions and 12 deletions

View File

@@ -118,9 +118,9 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
private volatile Object beanClass;
private String scope = SCOPE_SINGLETON;
private String scope;
private boolean singleton = true;
private boolean singleton = false;
private boolean prototype = false;
@@ -281,7 +281,9 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
if (other.getFactoryMethodName() != null) {
setFactoryMethodName(other.getFactoryMethodName());
}
setScope(other.getScope());
if (other.getScope() != null) {
setScope(other.getScope());
}
setAbstract(other.isAbstract());
setLazyInit(other.isLazyInit());
setRole(other.getRole());
@@ -408,7 +410,6 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
* @see #SCOPE_PROTOTYPE
*/
public void setScope(String scope) {
Assert.notNull(scope, "Scope must not be null");
this.scope = scope;
this.singleton = SCOPE_SINGLETON.equals(scope);
this.prototype = SCOPE_PROTOTYPE.equals(scope);

View File

@@ -1113,6 +1113,11 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
mbd.overrideFrom(bd);
}
// Set default singleton scope, if not configured before.
if (mbd.getScope() == null) {
mbd.setScope(RootBeanDefinition.SCOPE_SINGLETON);
}
// A bean contained in a non-singleton bean cannot be a singleton itself.
// Let's correct this on the fly here, since this might be the result of
// parent-child merging for the outer bean, in which case the original inner bean

View File

@@ -535,8 +535,7 @@ public class BeanDefinitionParserDelegate {
}
String lazyInit = ele.getAttribute(LAZY_INIT_ATTRIBUTE);
if (DEFAULT_VALUE.equals(lazyInit) && bd.isSingleton()) {
// Just apply default to singletons, as lazy-init has no meaning for prototypes.
if (DEFAULT_VALUE.equals(lazyInit)) {
lazyInit = this.defaults.getLazyInit();
}
bd.setLazyInit(TRUE_VALUE.equals(lazyInit));

View File

@@ -1972,7 +1972,7 @@ public final class DefaultListableBeanFactoryTests {
}
@Test
public void testImplicitScopeInheritanceForChildBeanDefinitions() throws Exception {
public void testScopeInheritanceForChildBeanDefinitions() throws Exception {
RootBeanDefinition parent = new RootBeanDefinition();
parent.setScope("bonanza!");
@@ -1984,7 +1984,7 @@ public final class DefaultListableBeanFactoryTests {
factory.registerBeanDefinition("child", child);
BeanDefinition def = factory.getMergedBeanDefinition("child");
assertTrue("Child 'scope' not overriding parent scope (it must).", def.isSingleton());
assertEquals("Child 'scope' not inherited", "bonanza!", def.getScope());
}
@Test

View File

@@ -3,7 +3,7 @@
<beans>
<bean id="inheritedTestBean" class="org.springframework.beans.TestBean" abstract="true">
<bean id="inheritedTestBean" class="org.springframework.beans.TestBean" abstract="true" scope="prototype">
<property name="name"><value>parent</value></property>
<property name="age"><value>1</value></property>
</bean>

View File

@@ -286,7 +286,7 @@ public final class XmlBeanFactoryTests {
}
}
public @Test void testSingletonInheritanceFromParentFactorySingleton() throws Exception {
public @Test void testInheritanceFromParentFactoryPrototype() throws Exception {
XmlBeanFactory parent = new XmlBeanFactory(PARENT_CONTEXT);
XmlBeanFactory child = new XmlBeanFactory(CHILD_CONTEXT, parent);
assertEquals(TestBean.class, child.getType("inheritsFromParentFactory"));
@@ -296,7 +296,7 @@ public final class XmlBeanFactoryTests {
// Age property is inherited from bean in parent factory
assertTrue(inherits.getAge() == 1);
TestBean inherits2 = (TestBean) child.getBean("inheritsFromParentFactory");
assertTrue(inherits2 == inherits);
assertFalse(inherits2 == inherits);
}
public @Test void testInheritanceWithDifferentClass() throws Exception {
@@ -420,7 +420,7 @@ public final class XmlBeanFactoryTests {
// Age property is inherited from bean in parent factory
assertTrue(inherits.getAge() == 1);
TestBean inherits2 = (TestBean) child.getBean("inheritedTestBean");
assertTrue(inherits2 == inherits);
assertTrue(inherits2 != inherits);
}
/**