From 01382b8ff0d5efe86d185fb3e8a07a23c6a195ed Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 31 Oct 2014 17:57:06 +0100 Subject: [PATCH] BeanDefinitionValueResolver preserves original String array if possible and resolves nested String arrays as well Issue: SPR-12391 --- .../support/BeanDefinitionValueResolver.java | 43 +++++++++++++------ 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/BeanDefinitionValueResolver.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/BeanDefinitionValueResolver.java index 084cd94f6a..81a9862a1c 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/BeanDefinitionValueResolver.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/BeanDefinitionValueResolver.java @@ -109,7 +109,7 @@ class BeanDefinitionValueResolver { } else if (value instanceof RuntimeBeanNameReference) { String refName = ((RuntimeBeanNameReference) value).getBeanName(); - refName = String.valueOf(evaluate(refName)); + refName = String.valueOf(doEvaluate(refName)); if (!this.beanFactory.containsBean(refName)) { throw new BeanDefinitionStoreException( "Invalid bean name '" + refName + "' in bean reference for " + argName); @@ -200,14 +200,6 @@ class BeanDefinitionValueResolver { "Error converting typed String value for " + argName, ex); } } - else if (value instanceof String[]) { - String[] values = (String[]) value; - Object[] resolvedValues = new Object[values.length]; - for (int i = 0; i < values.length; i++) { - resolvedValues[i] = evaluate(values[i]); - } - return resolvedValues; - } else { return evaluate(value); } @@ -219,7 +211,7 @@ class BeanDefinitionValueResolver { * @return the resolved value */ protected Object evaluate(TypedStringValue value) { - Object result = this.beanFactory.evaluateBeanDefinitionString(value.getValue(), this.beanDefinition); + Object result = doEvaluate(value.getValue()); if (!ObjectUtils.nullSafeEquals(result, value.getValue())) { value.setDynamic(); } @@ -228,18 +220,41 @@ class BeanDefinitionValueResolver { /** * Evaluate the given value as an expression, if necessary. - * @param value the candidate value (may be an expression) - * @return the resolved value + * @param value the original value (may be an expression) + * @return the resolved value if necessary, or the original value */ protected Object evaluate(Object value) { if (value instanceof String) { - return this.beanFactory.evaluateBeanDefinitionString((String) value, this.beanDefinition); + return doEvaluate((String) value); + } + else if (value instanceof String[]) { + String[] values = (String[]) value; + boolean actuallyResolved = false; + Object[] resolvedValues = new Object[values.length]; + for (int i = 0; i < values.length; i++) { + String originalValue = values[i]; + Object resolvedValue = doEvaluate(originalValue); + if (resolvedValue != originalValue) { + actuallyResolved = true; + } + resolvedValues[i] = resolvedValue; + } + return (actuallyResolved ? resolvedValues : values); } else { return value; } } + /** + * Evaluate the given String value as an expression, if necessary. + * @param value the original value (may be an expression) + * @return the resolved value if necessary, or the original String value + */ + private Object doEvaluate(String value) { + return this.beanFactory.evaluateBeanDefinitionString(value, this.beanDefinition); + } + /** * Resolve the target type in the given TypedStringValue. * @param value the TypedStringValue to resolve @@ -322,7 +337,7 @@ class BeanDefinitionValueResolver { private Object resolveReference(Object argName, RuntimeBeanReference ref) { try { String refName = ref.getBeanName(); - refName = String.valueOf(evaluate(refName)); + refName = String.valueOf(doEvaluate(refName)); if (ref.isToParent()) { if (this.beanFactory.getParentBeanFactory() == null) { throw new BeanCreationException(