prototype beans receive independent collection/array even when based on single value (SPR-5512)

This commit is contained in:
Juergen Hoeller 2009-02-21 18:04:08 +00:00
parent 2de9e2a38d
commit dc1edccc56
4 changed files with 63 additions and 7 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2009 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.
@ -26,6 +26,7 @@ import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
@ -1249,7 +1250,8 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
}
deepCopy.add(pv);
}
else if (originalValue instanceof TypedStringValue && convertible) {
else if (originalValue instanceof TypedStringValue && convertible &&
!(convertedValue instanceof Collection || ObjectUtils.isArray(convertedValue))) {
pv.setConvertedValue(convertedValue);
deepCopy.add(pv);
}

View File

@ -22,6 +22,7 @@ import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.TreeMap;
import java.util.Arrays;
import junit.framework.TestCase;
import test.beans.CustomEnum;
@ -158,6 +159,37 @@ public class UtilNamespaceHandlerTests extends TestCase {
Set innerSet = (Set) map.get("foo");
assertEquals(1, innerSet.size());
assertTrue(innerSet.contains("bar"));
TestBean bean2 = (TestBean) this.beanFactory.getBean("nestedCollectionsBean");
assertEquals(list, bean2.getSomeList());
assertEquals(set, bean2.getSomeSet());
assertEquals(map, bean2.getSomeMap());
assertFalse(list == bean2.getSomeList());
assertFalse(set == bean2.getSomeSet());
assertFalse(map == bean2.getSomeMap());
}
public void testNestedShortcutCollections() throws Exception {
TestBean bean = (TestBean) this.beanFactory.getBean("nestedShortcutCollections");
assertEquals(1, bean.getStringArray().length);
assertEquals("fooStr", bean.getStringArray()[0]);
List list = bean.getSomeList();
assertEquals(1, list.size());
assertEquals("foo", list.get(0));
Set set = bean.getSomeSet();
assertEquals(1, set.size());
assertTrue(set.contains("bar"));
TestBean bean2 = (TestBean) this.beanFactory.getBean("nestedShortcutCollections");
assertTrue(Arrays.equals(bean.getStringArray(), bean2.getStringArray()));
assertFalse(bean.getStringArray() == bean2.getStringArray());
assertEquals(list, bean2.getSomeList());
assertEquals(set, bean2.getSomeSet());
assertFalse(list == bean2.getSomeList());
assertFalse(set == bean2.getSomeSet());
}
public void testNestedInCollections() throws Exception {
@ -175,6 +207,14 @@ public class UtilNamespaceHandlerTests extends TestCase {
Map map = bean.getSomeMap();
assertEquals(1, map.size());
assertEquals(CustomEnum.VALUE_1, map.get("min"));
TestBean bean2 = (TestBean) this.beanFactory.getBean("nestedCustomTagBean");
assertEquals(list, bean2.getSomeList());
assertEquals(set, bean2.getSomeSet());
assertEquals(map, bean2.getSomeMap());
assertFalse(list == bean2.getSomeList());
assertFalse(set == bean2.getSomeSet());
assertFalse(map == bean2.getSomeMap());
}
public void testCircularCollections() throws Exception {

View File

@ -67,7 +67,7 @@
<value>Rob Harrop</value>
</util:set>
<bean id="nestedCollectionsBean" class="test.beans.TestBean">
<bean id="nestedCollectionsBean" class="test.beans.TestBean" scope="prototype">
<property name="someList">
<util:list>
<value>foo</value>
@ -89,6 +89,12 @@
</property>
</bean>
<bean id="nestedShortcutCollections" class="test.beans.TestBean" scope="prototype">
<property name="stringArray" value="fooStr"/>
<property name="someList" value="foo"/>
<property name="someSet" value="bar"/>
</bean>
<bean id="nestedCustomTagBean" class="test.beans.TestBean" scope="prototype">
<property name="someList">
<list>

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2009 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.
@ -82,10 +82,18 @@ public abstract class ObjectUtils {
}
/**
* Return whether the given array is empty: that is, <code>null</code>
* or of zero length.
* Determine whether the given object is an array:
* either an Object array or a primitive array.
* @param obj the object to check
*/
public static boolean isArray(Object obj) {
return (obj != null && obj.getClass().isArray());
}
/**
* Determine whether the given array is empty:
* i.e. <code>null</code> or of zero length.
* @param array the array to check
* @return whether the given array is empty
*/
public static boolean isEmpty(Object[] array) {
return (array == null || array.length == 0);