Browse Source

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

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@2363 50f2f4bb-b051-0410-bef5-90022cba6387
pull/1/head
Juergen Hoeller 16 years ago
parent
commit
ad0168d2f7
  1. 9
      org.springframework.beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanDefinition.java
  2. 5
      org.springframework.beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java
  3. 3
      org.springframework.beans/src/main/java/org/springframework/beans/factory/xml/BeanDefinitionParserDelegate.java
  4. 4
      org.springframework.beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java
  5. 2
      org.springframework.context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests-parent.xml
  6. 6
      org.springframework.context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests.java

9
org.springframework.beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanDefinition.java

@ -118,9 +118,9 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess @@ -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 @@ -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 @@ -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);

5
org.springframework.beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java

@ -1113,6 +1113,11 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp @@ -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

3
org.springframework.beans/src/main/java/org/springframework/beans/factory/xml/BeanDefinitionParserDelegate.java

@ -535,8 +535,7 @@ public class BeanDefinitionParserDelegate { @@ -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));

4
org.springframework.beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java

@ -1972,7 +1972,7 @@ public final class DefaultListableBeanFactoryTests { @@ -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 { @@ -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

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

@ -3,7 +3,7 @@ @@ -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>

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

@ -286,7 +286,7 @@ public final class XmlBeanFactoryTests { @@ -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 { @@ -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 { @@ -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);
}
/**

Loading…
Cancel
Save