From 6656a76cae3db020b400db217df697fb82d8c9fd Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Sat, 21 Feb 2009 18:04:08 +0000 Subject: [PATCH] prototype beans receive independent collection/array even when based on single value (SPR-5512) git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@663 50f2f4bb-b051-0410-bef5-90022cba6387 --- .../AbstractAutowireCapableBeanFactory.java | 6 ++- .../xml/UtilNamespaceHandlerTests.java | 40 +++++++++++++++++++ .../beans/factory/xml/testUtilNamespace.xml | 8 +++- .../org/springframework/util/ObjectUtils.java | 16 ++++++-- 4 files changed, 63 insertions(+), 7 deletions(-) diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java b/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java index a3273e0b47b..6db4d94f14c 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java @@ -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); } diff --git a/org.springframework.beans/src/test/java/org/springframework/beans/factory/xml/UtilNamespaceHandlerTests.java b/org.springframework.beans/src/test/java/org/springframework/beans/factory/xml/UtilNamespaceHandlerTests.java index bff9bd544e8..3345bf52fb3 100644 --- a/org.springframework.beans/src/test/java/org/springframework/beans/factory/xml/UtilNamespaceHandlerTests.java +++ b/org.springframework.beans/src/test/java/org/springframework/beans/factory/xml/UtilNamespaceHandlerTests.java @@ -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 { diff --git a/org.springframework.beans/src/test/resources/org/springframework/beans/factory/xml/testUtilNamespace.xml b/org.springframework.beans/src/test/resources/org/springframework/beans/factory/xml/testUtilNamespace.xml index e99c99d72e1..8426c5d95ee 100644 --- a/org.springframework.beans/src/test/resources/org/springframework/beans/factory/xml/testUtilNamespace.xml +++ b/org.springframework.beans/src/test/resources/org/springframework/beans/factory/xml/testUtilNamespace.xml @@ -67,7 +67,7 @@ Rob Harrop - + foo @@ -89,6 +89,12 @@ + + + + + + diff --git a/org.springframework.core/src/main/java/org/springframework/util/ObjectUtils.java b/org.springframework.core/src/main/java/org/springframework/util/ObjectUtils.java index f56cc9e0bed..9442226ed1a 100644 --- a/org.springframework.core/src/main/java/org/springframework/util/ObjectUtils.java +++ b/org.springframework.core/src/main/java/org/springframework/util/ObjectUtils.java @@ -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, null - * 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. null 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);