Skip to content

Commit 8e5c77d

Browse files
committed
Explicit support of String[] value resolution
This commit adds an explicit support for String array for value resolution. <util:properties> switches the 'locations' property to a String array in 662d8aa and this broke expression evaluation. Issue: SPR-12391
1 parent 2f03945 commit 8e5c77d

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/support/BeanDefinitionValueResolver.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,14 @@ else if (value instanceof TypedStringValue) {
200200
"Error converting typed String value for " + argName, ex);
201201
}
202202
}
203+
else if (value instanceof String[]) {
204+
String[] values = (String[]) value;
205+
Object[] resolvedValues = new Object[values.length];
206+
for (int i = 0; i < values.length; i++) {
207+
resolvedValues[i] = evaluate(values[i]);
208+
}
209+
return resolvedValues;
210+
}
203211
else {
204212
return evaluate(value);
205213
}

spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.junit.Rule;
4242
import org.junit.Test;
4343
import org.junit.rules.ExpectedException;
44+
import org.mockito.Matchers;
4445

4546
import org.springframework.beans.BeansException;
4647
import org.springframework.beans.MutablePropertyValues;
@@ -52,10 +53,13 @@
5253
import org.springframework.beans.TypeMismatchException;
5354
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
5455
import org.springframework.beans.factory.config.BeanDefinition;
56+
import org.springframework.beans.factory.config.BeanExpressionContext;
57+
import org.springframework.beans.factory.config.BeanExpressionResolver;
5558
import org.springframework.beans.factory.config.BeanPostProcessor;
5659
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
5760
import org.springframework.beans.factory.config.ConstructorArgumentValues;
5861
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;
62+
import org.springframework.beans.factory.config.PropertiesFactoryBean;
5963
import org.springframework.beans.factory.config.RuntimeBeanReference;
6064
import org.springframework.beans.factory.config.TypedStringValue;
6165
import org.springframework.beans.factory.support.AbstractBeanDefinition;
@@ -1165,6 +1169,23 @@ public void testDoubleArrayConstructorWithOptionalAutowiring() throws MalformedU
11651169
assertNull(ab.getResourceArray());
11661170
}
11671171

1172+
@Test
1173+
public void testExpressionInStringArray() {
1174+
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
1175+
BeanExpressionResolver beanExpressionResolver = mock(BeanExpressionResolver.class);
1176+
when(beanExpressionResolver.evaluate(eq("#{foo}"), Matchers.any(BeanExpressionContext.class)))
1177+
.thenReturn("classpath:/org/springframework/beans/factory/xml/util.properties");
1178+
bf.setBeanExpressionResolver(beanExpressionResolver);
1179+
1180+
RootBeanDefinition rbd = new RootBeanDefinition(PropertiesFactoryBean.class);
1181+
MutablePropertyValues pvs = new MutablePropertyValues();
1182+
pvs.add("locations", new String[]{"#{foo}"});
1183+
rbd.setPropertyValues(pvs);
1184+
bf.registerBeanDefinition("myProperties", rbd);
1185+
Properties properties = (Properties) bf.getBean("myProperties");
1186+
assertEquals("bar", properties.getProperty("foo"));
1187+
}
1188+
11681189
@Test
11691190
public void testAutowireWithNoDependencies() {
11701191
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();

0 commit comments

Comments
 (0)