Fixed type detection to avoid reuse of parent bean's targetType on child definition merge
Issue: SPR-10374
This commit is contained in:
		
							parent
							
								
									3f7007f73a
								
							
						
					
					
						commit
						070103b17e
					
				| 
						 | 
				
			
			@ -308,12 +308,12 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
 | 
			
		|||
		RootBeanDefinition bd = null;
 | 
			
		||||
		if (mbd instanceof RootBeanDefinition) {
 | 
			
		||||
			RootBeanDefinition rbd = (RootBeanDefinition) mbd;
 | 
			
		||||
			if (rbd.isPrototype()) {
 | 
			
		||||
				bd = rbd;
 | 
			
		||||
			}
 | 
			
		||||
			bd = (rbd.isPrototype() ? rbd : rbd.cloneBeanDefinition());
 | 
			
		||||
		}
 | 
			
		||||
		if (bd == null) {
 | 
			
		||||
			bd = new RootBeanDefinition(mbd);
 | 
			
		||||
		if (!mbd.isPrototype()) {
 | 
			
		||||
			if (bd == null) {
 | 
			
		||||
				bd = new RootBeanDefinition(mbd);
 | 
			
		||||
			}
 | 
			
		||||
			bd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
 | 
			
		||||
			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");
 | 
			
		||||
 * you may not use this file except in compliance with the License.
 | 
			
		||||
| 
						 | 
				
			
			@ -17,7 +17,6 @@
 | 
			
		|||
package org.springframework.beans.factory.support;
 | 
			
		||||
 | 
			
		||||
import org.springframework.beans.MutablePropertyValues;
 | 
			
		||||
import org.springframework.beans.factory.config.BeanDefinition;
 | 
			
		||||
import org.springframework.beans.factory.config.ConstructorArgumentValues;
 | 
			
		||||
import org.springframework.util.ObjectUtils;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -98,7 +97,7 @@ public class ChildBeanDefinition extends AbstractBeanDefinition {
 | 
			
		|||
	 * @param pvs the property values to apply
 | 
			
		||||
	 */
 | 
			
		||||
	public ChildBeanDefinition(
 | 
			
		||||
			String parentName, Class beanClass, ConstructorArgumentValues cargs, MutablePropertyValues pvs) {
 | 
			
		||||
			String parentName, Class<?> beanClass, ConstructorArgumentValues cargs, MutablePropertyValues pvs) {
 | 
			
		||||
 | 
			
		||||
		super(cargs, pvs);
 | 
			
		||||
		this.parentName = parentName;
 | 
			
		||||
| 
						 | 
				
			
			@ -128,7 +127,7 @@ public class ChildBeanDefinition extends AbstractBeanDefinition {
 | 
			
		|||
	 * @param original the original bean definition to copy from
 | 
			
		||||
	 */
 | 
			
		||||
	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
 | 
			
		||||
	 */
 | 
			
		||||
	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) {
 | 
			
		||||
		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"));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@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
 | 
			
		||||
	public void testNameAlreadyBound() {
 | 
			
		||||
		DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue