child bean definition's scope attribute can be inherited from parent bean definition now (SPR-3542)
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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));
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user