Honour @MultipartConfig on servlets registered via @ServletComponentScan
Closes gh-6680
This commit is contained in:
parent
a6f777e941
commit
a2420bacfb
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2015 the original author or authors.
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -20,7 +20,6 @@ import java.lang.annotation.Annotation;
|
|||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.context.annotation.ScannedGenericBeanDefinition;
|
||||
import org.springframework.core.annotation.AnnotationAttributes;
|
||||
|
|
@ -83,6 +82,6 @@ abstract class ServletComponentHandler {
|
|||
}
|
||||
|
||||
protected abstract void doHandle(Map<String, Object> attributes,
|
||||
BeanDefinition beanDefinition, BeanDefinitionRegistry registry);
|
||||
ScannedGenericBeanDefinition beanDefinition, BeanDefinitionRegistry registry);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ import javax.servlet.annotation.WebFilter;
|
|||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.context.annotation.ScannedGenericBeanDefinition;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
|
|
@ -40,7 +41,8 @@ class WebFilterHandler extends ServletComponentHandler {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void doHandle(Map<String, Object> attributes, BeanDefinition beanDefinition,
|
||||
public void doHandle(Map<String, Object> attributes,
|
||||
ScannedGenericBeanDefinition beanDefinition,
|
||||
BeanDefinitionRegistry registry) {
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder
|
||||
.rootBeanDefinition(FilterRegistrationBean.class);
|
||||
|
|
|
|||
|
|
@ -20,9 +20,9 @@ import java.util.Map;
|
|||
|
||||
import javax.servlet.annotation.WebListener;
|
||||
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.context.annotation.ScannedGenericBeanDefinition;
|
||||
|
||||
/**
|
||||
* Handler for {@link WebListener}-annotated classes.
|
||||
|
|
@ -36,7 +36,8 @@ class WebListenerHandler extends ServletComponentHandler {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected void doHandle(Map<String, Object> attributes, BeanDefinition beanDefinition,
|
||||
protected void doHandle(Map<String, Object> attributes,
|
||||
ScannedGenericBeanDefinition beanDefinition,
|
||||
BeanDefinitionRegistry registry) {
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder
|
||||
.rootBeanDefinition(ServletListenerRegistrationBean.class);
|
||||
|
|
|
|||
|
|
@ -18,11 +18,14 @@ package org.springframework.boot.web.servlet;
|
|||
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.MultipartConfigElement;
|
||||
import javax.servlet.annotation.MultipartConfig;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.context.annotation.ScannedGenericBeanDefinition;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
|
|
@ -37,7 +40,8 @@ class WebServletHandler extends ServletComponentHandler {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void doHandle(Map<String, Object> attributes, BeanDefinition beanDefinition,
|
||||
public void doHandle(Map<String, Object> attributes,
|
||||
ScannedGenericBeanDefinition beanDefinition,
|
||||
BeanDefinitionRegistry registry) {
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder
|
||||
.rootBeanDefinition(ServletRegistrationBean.class);
|
||||
|
|
@ -49,6 +53,8 @@ class WebServletHandler extends ServletComponentHandler {
|
|||
builder.addPropertyValue("servlet", beanDefinition);
|
||||
builder.addPropertyValue("urlMappings",
|
||||
extractUrlPatterns("urlPatterns", attributes));
|
||||
builder.addPropertyValue("multipartConfig",
|
||||
determineMultipartConfig(beanDefinition));
|
||||
registry.registerBeanDefinition(name, builder.getBeanDefinition());
|
||||
}
|
||||
|
||||
|
|
@ -58,4 +64,17 @@ class WebServletHandler extends ServletComponentHandler {
|
|||
? attributes.get("name") : beanDefinition.getBeanClassName());
|
||||
}
|
||||
|
||||
private MultipartConfigElement determineMultipartConfig(
|
||||
ScannedGenericBeanDefinition beanDefinition) {
|
||||
Map<String, Object> attributes = beanDefinition.getMetadata()
|
||||
.getAnnotationAttributes(MultipartConfig.class.getName());
|
||||
if (attributes == null) {
|
||||
return null;
|
||||
}
|
||||
return new MultipartConfigElement((String) attributes.get("location"),
|
||||
(long) attributes.get("maxFileSize"),
|
||||
(long) attributes.get("maxRequestSize"),
|
||||
(int) attributes.get("fileSizeThreshold"));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,12 +16,17 @@
|
|||
|
||||
package org.springframework.boot.web.servlet;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.MultipartConfigElement;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext;
|
||||
import org.springframework.boot.context.embedded.ServerPortInfoApplicationContextInitializer;
|
||||
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
|
||||
import org.springframework.boot.web.servlet.testcomponents.TestMultipartServlet;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
|
@ -56,6 +61,26 @@ public class ServletComponentScanIntegrationTests {
|
|||
assertThat(response).isEqualTo("alpha bravo");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multipartConfigIsHonoured() {
|
||||
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
|
||||
this.context.register(TestConfiguration.class);
|
||||
new ServerPortInfoApplicationContextInitializer().initialize(this.context);
|
||||
this.context.refresh();
|
||||
Map<String, ServletRegistrationBean> beans = this.context
|
||||
.getBeansOfType(ServletRegistrationBean.class);
|
||||
ServletRegistrationBean servletRegistrationBean = beans
|
||||
.get(TestMultipartServlet.class.getName());
|
||||
assertThat(servletRegistrationBean).isNotNull();
|
||||
MultipartConfigElement multipartConfig = servletRegistrationBean
|
||||
.getMultipartConfig();
|
||||
assertThat(multipartConfig).isNotNull();
|
||||
assertThat(multipartConfig.getLocation()).isEqualTo("test");
|
||||
assertThat(multipartConfig.getMaxRequestSize()).isEqualTo(2048);
|
||||
assertThat(multipartConfig.getMaxFileSize()).isEqualTo(1024);
|
||||
assertThat(multipartConfig.getFileSizeThreshold()).isEqualTo(512);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ServletComponentScan(basePackages = "org.springframework.boot.web.servlet.testcomponents")
|
||||
static class TestConfiguration {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,27 @@
|
|||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.web.servlet.testcomponents;
|
||||
|
||||
import javax.servlet.annotation.MultipartConfig;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
|
||||
@WebServlet("/test-multipart")
|
||||
@MultipartConfig(location = "test", maxFileSize = 1024, maxRequestSize = 2048, fileSizeThreshold = 512)
|
||||
public class TestMultipartServlet extends HttpServlet {
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue