Skip to content

Commit 070103b

Browse files
committed
Fixed type detection to avoid reuse of parent bean's targetType on child definition merge
Issue: SPR-10374
1 parent 3f7007f commit 070103b

File tree

4 files changed

+27
-17
lines changed

4 files changed

+27
-17
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -308,12 +308,12 @@ public Object configureBean(Object existingBean, String beanName) throws BeansEx
308308
RootBeanDefinition bd = null;
309309
if (mbd instanceof RootBeanDefinition) {
310310
RootBeanDefinition rbd = (RootBeanDefinition) mbd;
311-
if (rbd.isPrototype()) {
312-
bd = rbd;
313-
}
311+
bd = (rbd.isPrototype() ? rbd : rbd.cloneBeanDefinition());
314312
}
315-
if (bd == null) {
316-
bd = new RootBeanDefinition(mbd);
313+
if (!mbd.isPrototype()) {
314+
if (bd == null) {
315+
bd = new RootBeanDefinition(mbd);
316+
}
317317
bd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
318318
bd.allowCaching = false;
319319
}

spring-beans/src/main/java/org/springframework/beans/factory/support/ChildBeanDefinition.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,7 +17,6 @@
1717
package org.springframework.beans.factory.support;
1818

1919
import org.springframework.beans.MutablePropertyValues;
20-
import org.springframework.beans.factory.config.BeanDefinition;
2120
import org.springframework.beans.factory.config.ConstructorArgumentValues;
2221
import org.springframework.util.ObjectUtils;
2322

@@ -98,7 +97,7 @@ public ChildBeanDefinition(
9897
* @param pvs the property values to apply
9998
*/
10099
public ChildBeanDefinition(
101-
String parentName, Class beanClass, ConstructorArgumentValues cargs, MutablePropertyValues pvs) {
100+
String parentName, Class<?> beanClass, ConstructorArgumentValues cargs, MutablePropertyValues pvs) {
102101

103102
super(cargs, pvs);
104103
this.parentName = parentName;
@@ -128,7 +127,7 @@ public ChildBeanDefinition(
128127
* @param original the original bean definition to copy from
129128
*/
130129
public ChildBeanDefinition(ChildBeanDefinition original) {
131-
super((BeanDefinition) original);
130+
super(original);
132131
}
133132

134133

spring-beans/src/main/java/org/springframework/beans/factory/support/RootBeanDefinition.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,11 @@ public RootBeanDefinition(String beanClassName, ConstructorArgumentValues cargs,
171171
* @param original the original bean definition to copy from
172172
*/
173173
public RootBeanDefinition(RootBeanDefinition original) {
174-
this((BeanDefinition) original);
174+
super(original);
175+
this.decoratedDefinition = original.decoratedDefinition;
176+
this.allowCaching = original.allowCaching;
177+
this.targetType = original.targetType;
178+
this.isFactoryMethodUnique = original.isFactoryMethodUnique;
175179
}
176180

177181
/**
@@ -181,13 +185,6 @@ public RootBeanDefinition(RootBeanDefinition original) {
181185
*/
182186
RootBeanDefinition(BeanDefinition original) {
183187
super(original);
184-
if (original instanceof RootBeanDefinition) {
185-
RootBeanDefinition originalRbd = (RootBeanDefinition) original;
186-
this.decoratedDefinition = originalRbd.decoratedDefinition;
187-
this.allowCaching = originalRbd.allowCaching;
188-
this.targetType = originalRbd.targetType;
189-
this.isFactoryMethodUnique = originalRbd.isFactoryMethodUnique;
190-
}
191188
}
192189

193190

spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,20 @@ public void testCanReferenceParentBeanFromChildViaAlias() {
712712
factory.getMergedBeanDefinition("child"), factory.getMergedBeanDefinition("child"));
713713
}
714714

715+
@Test
716+
public void testGetTypeWorksAfterParentChildMerging() {
717+
RootBeanDefinition parentDefinition = new RootBeanDefinition(TestBean.class);
718+
ChildBeanDefinition childDefinition = new ChildBeanDefinition("parent", DerivedTestBean.class, null, null);
719+
720+
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
721+
factory.registerBeanDefinition("parent", parentDefinition);
722+
factory.registerBeanDefinition("child", childDefinition);
723+
factory.freezeConfiguration();
724+
725+
assertEquals(TestBean.class, factory.getType("parent"));
726+
assertEquals(DerivedTestBean.class, factory.getType("child"));
727+
}
728+
715729
@Test
716730
public void testNameAlreadyBound() {
717731
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();

0 commit comments

Comments
 (0)