Fixed type detection to avoid reuse of parent bean's targetType on child definition merge
Issue: SPR-10374
This commit is contained in:
@@ -308,12 +308,12 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
|
|||||||
RootBeanDefinition bd = null;
|
RootBeanDefinition bd = null;
|
||||||
if (mbd instanceof RootBeanDefinition) {
|
if (mbd instanceof RootBeanDefinition) {
|
||||||
RootBeanDefinition rbd = (RootBeanDefinition) mbd;
|
RootBeanDefinition rbd = (RootBeanDefinition) mbd;
|
||||||
if (rbd.isPrototype()) {
|
bd = (rbd.isPrototype() ? rbd : rbd.cloneBeanDefinition());
|
||||||
bd = rbd;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (bd == null) {
|
if (!mbd.isPrototype()) {
|
||||||
bd = new RootBeanDefinition(mbd);
|
if (bd == null) {
|
||||||
|
bd = new RootBeanDefinition(mbd);
|
||||||
|
}
|
||||||
bd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
|
bd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
|
||||||
bd.allowCaching = false;
|
bd.allowCaching = false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2012 the original author or authors.
|
* Copyright 2002-2013 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -17,7 +17,6 @@
|
|||||||
package org.springframework.beans.factory.support;
|
package org.springframework.beans.factory.support;
|
||||||
|
|
||||||
import org.springframework.beans.MutablePropertyValues;
|
import org.springframework.beans.MutablePropertyValues;
|
||||||
import org.springframework.beans.factory.config.BeanDefinition;
|
|
||||||
import org.springframework.beans.factory.config.ConstructorArgumentValues;
|
import org.springframework.beans.factory.config.ConstructorArgumentValues;
|
||||||
import org.springframework.util.ObjectUtils;
|
import org.springframework.util.ObjectUtils;
|
||||||
|
|
||||||
@@ -98,7 +97,7 @@ public class ChildBeanDefinition extends AbstractBeanDefinition {
|
|||||||
* @param pvs the property values to apply
|
* @param pvs the property values to apply
|
||||||
*/
|
*/
|
||||||
public ChildBeanDefinition(
|
public ChildBeanDefinition(
|
||||||
String parentName, Class beanClass, ConstructorArgumentValues cargs, MutablePropertyValues pvs) {
|
String parentName, Class<?> beanClass, ConstructorArgumentValues cargs, MutablePropertyValues pvs) {
|
||||||
|
|
||||||
super(cargs, pvs);
|
super(cargs, pvs);
|
||||||
this.parentName = parentName;
|
this.parentName = parentName;
|
||||||
@@ -128,7 +127,7 @@ public class ChildBeanDefinition extends AbstractBeanDefinition {
|
|||||||
* @param original the original bean definition to copy from
|
* @param original the original bean definition to copy from
|
||||||
*/
|
*/
|
||||||
public ChildBeanDefinition(ChildBeanDefinition original) {
|
public ChildBeanDefinition(ChildBeanDefinition original) {
|
||||||
super((BeanDefinition) original);
|
super(original);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -171,7 +171,11 @@ public class RootBeanDefinition extends AbstractBeanDefinition {
|
|||||||
* @param original the original bean definition to copy from
|
* @param original the original bean definition to copy from
|
||||||
*/
|
*/
|
||||||
public RootBeanDefinition(RootBeanDefinition original) {
|
public RootBeanDefinition(RootBeanDefinition original) {
|
||||||
this((BeanDefinition) original);
|
super(original);
|
||||||
|
this.decoratedDefinition = original.decoratedDefinition;
|
||||||
|
this.allowCaching = original.allowCaching;
|
||||||
|
this.targetType = original.targetType;
|
||||||
|
this.isFactoryMethodUnique = original.isFactoryMethodUnique;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -181,13 +185,6 @@ public class RootBeanDefinition extends AbstractBeanDefinition {
|
|||||||
*/
|
*/
|
||||||
RootBeanDefinition(BeanDefinition original) {
|
RootBeanDefinition(BeanDefinition original) {
|
||||||
super(original);
|
super(original);
|
||||||
if (original instanceof RootBeanDefinition) {
|
|
||||||
RootBeanDefinition originalRbd = (RootBeanDefinition) original;
|
|
||||||
this.decoratedDefinition = originalRbd.decoratedDefinition;
|
|
||||||
this.allowCaching = originalRbd.allowCaching;
|
|
||||||
this.targetType = originalRbd.targetType;
|
|
||||||
this.isFactoryMethodUnique = originalRbd.isFactoryMethodUnique;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -712,6 +712,20 @@ public class DefaultListableBeanFactoryTests {
|
|||||||
factory.getMergedBeanDefinition("child"), factory.getMergedBeanDefinition("child"));
|
factory.getMergedBeanDefinition("child"), factory.getMergedBeanDefinition("child"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetTypeWorksAfterParentChildMerging() {
|
||||||
|
RootBeanDefinition parentDefinition = new RootBeanDefinition(TestBean.class);
|
||||||
|
ChildBeanDefinition childDefinition = new ChildBeanDefinition("parent", DerivedTestBean.class, null, null);
|
||||||
|
|
||||||
|
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
|
||||||
|
factory.registerBeanDefinition("parent", parentDefinition);
|
||||||
|
factory.registerBeanDefinition("child", childDefinition);
|
||||||
|
factory.freezeConfiguration();
|
||||||
|
|
||||||
|
assertEquals(TestBean.class, factory.getType("parent"));
|
||||||
|
assertEquals(DerivedTestBean.class, factory.getType("child"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNameAlreadyBound() {
|
public void testNameAlreadyBound() {
|
||||||
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
|
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
|
||||||
|
|||||||
Reference in New Issue
Block a user