From a0ba8c90a6dac9f8889e54207619a47e881da9e0 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Wed, 5 Mar 2014 09:30:18 +0000 Subject: [PATCH] Add MultipartConfig to DispatcherServlet by default RC4 added a ServletRegistrationBaen for teh DispatcherServlet and we didn't register a MultipartConfigElement if one was present. Fixes gh-427 --- .../DispatcherServletAutoConfiguration.java | 12 +++++-- ...spatcherServletAutoConfigurationTests.java | 33 +++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/DispatcherServletAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/DispatcherServletAutoConfiguration.java index da8602716a..52ef8d65ad 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/DispatcherServletAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/DispatcherServletAutoConfiguration.java @@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.web; import java.util.Arrays; import java.util.List; +import javax.servlet.MultipartConfigElement; import javax.servlet.ServletRegistration; import org.springframework.beans.factory.annotation.Autowired; @@ -69,6 +70,9 @@ public class DispatcherServletAutoConfiguration { @Autowired private ServerProperties server; + @Autowired(required = false) + private MultipartConfigElement multipartConfig; + @Bean(name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME) public DispatcherServlet dispatcherServlet() { return new DispatcherServlet(); @@ -76,8 +80,12 @@ public class DispatcherServletAutoConfiguration { @Bean public ServletRegistrationBean dispatcherServletRegistration() { - return new ServletRegistrationBean(dispatcherServlet(), - this.server.getServletPath()); + ServletRegistrationBean registration = new ServletRegistrationBean( + dispatcherServlet(), this.server.getServletPath()); + if (this.multipartConfig != null) { + registration.setMultipartConfig(this.multipartConfig); + } + return registration; } } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/DispatcherServletAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/DispatcherServletAutoConfigurationTests.java index baf1a47c6f..6f54501f6e 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/DispatcherServletAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/DispatcherServletAutoConfigurationTests.java @@ -16,15 +16,21 @@ package org.springframework.boot.autoconfigure.web; +import javax.servlet.MultipartConfigElement; + import org.junit.Test; +import org.springframework.boot.context.embedded.MultiPartConfigFactory; import org.springframework.boot.context.embedded.ServletRegistrationBean; import org.springframework.boot.test.EnvironmentTestUtils; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; import org.springframework.mock.web.MockServletContext; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.servlet.DispatcherServlet; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; /** * Tests for {@link DispatcherServletAutoConfiguration}. @@ -60,6 +66,33 @@ public class DispatcherServletAutoConfigurationTests { ServletRegistrationBean registration = this.context .getBean(ServletRegistrationBean.class); assertEquals("[/spring]", registration.getUrlMappings().toString()); + assertNull(registration.getMultipartConfig()); + } + + @Test + public void multipartConfig() throws Exception { + this.context = new AnnotationConfigWebApplicationContext(); + this.context.setServletContext(new MockServletContext()); + this.context.register(MultipartConfiguration.class, + ServerPropertiesAutoConfiguration.class, + DispatcherServletAutoConfiguration.class); + this.context.refresh(); + ServletRegistrationBean registration = this.context + .getBean(ServletRegistrationBean.class); + assertNotNull(registration.getMultipartConfig()); + } + + @Configuration + protected static class MultipartConfiguration { + + @Bean + public MultipartConfigElement multipartConfig() { + MultiPartConfigFactory factory = new MultiPartConfigFactory(); + factory.setMaxFileSize("128KB"); + factory.setMaxRequestSize("128KB"); + return factory.createMultipartConfig(); + } + } }