Browse Source

Allow nulls with multiple embedded value resolvers

Allow an embedded value resolver added to an AbstractBeanFactory to
return null without adversely effecting any subsequent resolvers.

Issue: SPR-8565
pull/222/head
Phillip Webb 13 years ago
parent
commit
f3ff98d862
  1. 2
      spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java
  2. 26
      spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java

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

@ -746,7 +746,7 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp @@ -746,7 +746,7 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
public String resolveEmbeddedValue(String value) {
String result = value;
for (StringValueResolver resolver : this.embeddedValueResolvers) {
result = resolver.resolveStringValue(result);
result = (result == null ? null : resolver.resolveStringValue(result));
}
return result;
}

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

@ -28,6 +28,11 @@ import static org.junit.Assert.assertSame; @@ -28,6 +28,11 @@ import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.isNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import java.io.Closeable;
import java.lang.reflect.Field;
@ -97,6 +102,7 @@ import org.springframework.tests.sample.beans.SideEffectBean; @@ -97,6 +102,7 @@ import org.springframework.tests.sample.beans.SideEffectBean;
import org.springframework.tests.sample.beans.TestBean;
import org.springframework.tests.sample.beans.factory.DummyFactory;
import org.springframework.util.StopWatch;
import org.springframework.util.StringValueResolver;
/**
* Tests properties population and autowire behavior.
@ -2261,6 +2267,26 @@ public class DefaultListableBeanFactoryTests { @@ -2261,6 +2267,26 @@ public class DefaultListableBeanFactoryTests {
assertThat(bf.containsBean("bogus"), is(false));
}
@Test
public void resolveEmbeddedValue() throws Exception {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
StringValueResolver r1 = mock(StringValueResolver.class);
StringValueResolver r2 = mock(StringValueResolver.class);
StringValueResolver r3 = mock(StringValueResolver.class);
bf.addEmbeddedValueResolver(r1);
bf.addEmbeddedValueResolver(r2);
bf.addEmbeddedValueResolver(r3);
given(r1.resolveStringValue("A")).willReturn("B");
given(r2.resolveStringValue("B")).willReturn(null);
given(r3.resolveStringValue(isNull(String.class))).willThrow(new IllegalArgumentException());
bf.resolveEmbeddedValue("A");
verify(r1).resolveStringValue("A");
verify(r2).resolveStringValue("B");
verify(r3, never()).resolveStringValue(isNull(String.class));
}
static class A { }
static class B { }

Loading…
Cancel
Save