Detect existing MultipartResolver beans

Update MultipartAutoConfiguration to detect any MultipartResolver beans
rather than just StandardServletMultipartResolvers.

Fixes gh-1857
pull/2035/head
Phillip Webb 10 years ago
parent 322bdd62ef
commit b79934a7a4

@ -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();
}

@ -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 {

Loading…
Cancel
Save