From b79934a7a49b2265f4625898b43695fbd89992cb Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 10 Nov 2014 17:21:21 -0800 Subject: [PATCH] Detect existing MultipartResolver beans Update MultipartAutoConfiguration to detect any MultipartResolver beans rather than just StandardServletMultipartResolvers. Fixes gh-1857 --- .../web/MultipartAutoConfiguration.java | 3 ++- .../web/MultipartAutoConfigurationTests.java | 22 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/MultipartAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/MultipartAutoConfiguration.java index ba28377b85..94a0a5860d 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/MultipartAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/MultipartAutoConfiguration.java @@ -28,6 +28,7 @@ import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.web.multipart.MultipartResolver; import org.springframework.web.multipart.support.StandardServletMultipartResolver; /** @@ -60,7 +61,7 @@ public class MultipartAutoConfiguration { } @Bean - @ConditionalOnMissingBean + @ConditionalOnMissingBean(value = MultipartResolver.class) public StandardServletMultipartResolver multipartResolver() { return new StandardServletMultipartResolver(); } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/MultipartAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/MultipartAutoConfigurationTests.java index 11e0de9394..3d047230d7 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/MultipartAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/MultipartAutoConfigurationTests.java @@ -39,11 +39,14 @@ import org.springframework.web.servlet.DispatcherServlet; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.instanceOf; +import static org.hamcrest.Matchers.not; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; /** * Tests for {@link MultipartAutoConfiguration}. Tests an empty configuration, no @@ -165,6 +168,16 @@ public class MultipartAutoConfigurationTests { assertEquals(0, this.context.getBeansOfType(MultipartConfigElement.class).size()); } + @Test + public void containerWithCustomMulipartResolver() throws Exception { + this.context = new AnnotationConfigEmbeddedWebApplicationContext( + ContainerWithCustomMultipartResolver.class, BaseConfiguration.class); + MultipartResolver multipartResolver = this.context + .getBean(MultipartResolver.class); + assertThat(multipartResolver, + not(instanceOf(StandardServletMultipartResolver.class))); + } + private void verifyServletWorks() { RestTemplate restTemplate = new RestTemplate(); assertEquals("Hello", restTemplate.getForObject("http://localhost:" @@ -243,6 +256,15 @@ public class MultipartAutoConfigurationTests { } + public static class ContainerWithCustomMultipartResolver { + + @Bean + MultipartResolver multipartResolver() { + return mock(MultipartResolver.class); + } + + } + @Controller public static class WebController {