diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanDefinition.java b/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanDefinition.java index 9f28b41d17e..36883a9b051 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanDefinition.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanDefinition.java @@ -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); diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java b/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java index 03155c4edf3..3cd83fefaf6 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java @@ -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 diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/factory/xml/BeanDefinitionParserDelegate.java b/org.springframework.beans/src/main/java/org/springframework/beans/factory/xml/BeanDefinitionParserDelegate.java index 5d1f7df10b4..c28f0b18a28 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/factory/xml/BeanDefinitionParserDelegate.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/factory/xml/BeanDefinitionParserDelegate.java @@ -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)); diff --git a/org.springframework.beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java b/org.springframework.beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java index c0786cf3951..0e6e86e597b 100644 --- a/org.springframework.beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java +++ b/org.springframework.beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java @@ -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 diff --git a/org.springframework.context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests-parent.xml b/org.springframework.context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests-parent.xml index 6f5675fffd6..98f23096735 100644 --- a/org.springframework.context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests-parent.xml +++ b/org.springframework.context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests-parent.xml @@ -3,7 +3,7 @@ - + parent 1 diff --git a/org.springframework.context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests.java b/org.springframework.context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests.java index 7081db7cbda..5d33e0356f2 100644 --- a/org.springframework.context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests.java +++ b/org.springframework.context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests.java @@ -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); } /**