From 8e5c77dc1191432d8a9e0109beb4e9d3f925e8d2 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 31 Oct 2014 07:55:27 +0100 Subject: [PATCH] Explicit support of String[] value resolution This commit adds an explicit support for String array for value resolution. switches the 'locations' property to a String array in 662d8aa and this broke expression evaluation. Issue: SPR-12391 --- .../support/BeanDefinitionValueResolver.java | 8 +++++++ .../DefaultListableBeanFactoryTests.java | 21 +++++++++++++++++++ 2 files changed, 29 insertions(+) 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 18aaa23d03..084cd94f6a 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 @@ -200,6 +200,14 @@ 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); } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java index cc20a21243..767961d3bb 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java @@ -41,6 +41,7 @@ import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.mockito.Matchers; import org.springframework.beans.BeansException; import org.springframework.beans.MutablePropertyValues; @@ -52,10 +53,13 @@ import org.springframework.beans.TypeConverter; import org.springframework.beans.TypeMismatchException; import org.springframework.beans.factory.config.AutowireCapableBeanFactory; import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.beans.factory.config.BeanExpressionContext; +import org.springframework.beans.factory.config.BeanExpressionResolver; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.beans.factory.config.ConstructorArgumentValues; import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter; +import org.springframework.beans.factory.config.PropertiesFactoryBean; import org.springframework.beans.factory.config.RuntimeBeanReference; import org.springframework.beans.factory.config.TypedStringValue; import org.springframework.beans.factory.support.AbstractBeanDefinition; @@ -1165,6 +1169,23 @@ public class DefaultListableBeanFactoryTests { assertNull(ab.getResourceArray()); } + @Test + public void testExpressionInStringArray() { + DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); + BeanExpressionResolver beanExpressionResolver = mock(BeanExpressionResolver.class); + when(beanExpressionResolver.evaluate(eq("#{foo}"), Matchers.any(BeanExpressionContext.class))) + .thenReturn("classpath:/org/springframework/beans/factory/xml/util.properties"); + bf.setBeanExpressionResolver(beanExpressionResolver); + + RootBeanDefinition rbd = new RootBeanDefinition(PropertiesFactoryBean.class); + MutablePropertyValues pvs = new MutablePropertyValues(); + pvs.add("locations", new String[]{"#{foo}"}); + rbd.setPropertyValues(pvs); + bf.registerBeanDefinition("myProperties", rbd); + Properties properties = (Properties) bf.getBean("myProperties"); + assertEquals("bar", properties.getProperty("foo")); + } + @Test public void testAutowireWithNoDependencies() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();