001/* 002 * Copyright 2012-2016 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.boot.autoconfigure.web; 018 019import javax.servlet.MultipartConfigElement; 020import javax.servlet.Servlet; 021 022import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 023import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 024import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 025import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 026import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext; 027import org.springframework.boot.context.properties.EnableConfigurationProperties; 028import org.springframework.context.annotation.Bean; 029import org.springframework.context.annotation.Configuration; 030import org.springframework.web.multipart.MultipartResolver; 031import org.springframework.web.multipart.support.StandardServletMultipartResolver; 032import org.springframework.web.servlet.DispatcherServlet; 033 034/** 035 * {@link EnableAutoConfiguration Auto-configuration} for multi-part uploads. Adds a 036 * {@link StandardServletMultipartResolver} if none is present, and adds a 037 * {@link javax.servlet.MultipartConfigElement multipartConfigElement} if none is 038 * otherwise defined. The {@link EmbeddedWebApplicationContext} will associate the 039 * {@link MultipartConfigElement} bean to any {@link Servlet} beans. 040 * <p> 041 * The {@link javax.servlet.MultipartConfigElement} is a Servlet API that's used to 042 * configure how the container handles file uploads. By default 043 * 044 * @author Greg Turnquist 045 * @author Josh Long 046 * @author Toshiaki Maki 047 */ 048@Configuration 049@ConditionalOnClass({ Servlet.class, StandardServletMultipartResolver.class, 050 MultipartConfigElement.class }) 051@ConditionalOnProperty(prefix = "spring.http.multipart", name = "enabled", matchIfMissing = true) 052@EnableConfigurationProperties(MultipartProperties.class) 053public class MultipartAutoConfiguration { 054 055 private final MultipartProperties multipartProperties; 056 057 public MultipartAutoConfiguration(MultipartProperties multipartProperties) { 058 this.multipartProperties = multipartProperties; 059 } 060 061 @Bean 062 @ConditionalOnMissingBean 063 public MultipartConfigElement multipartConfigElement() { 064 return this.multipartProperties.createMultipartConfig(); 065 } 066 067 @Bean(name = DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME) 068 @ConditionalOnMissingBean(MultipartResolver.class) 069 public StandardServletMultipartResolver multipartResolver() { 070 StandardServletMultipartResolver multipartResolver = new StandardServletMultipartResolver(); 071 multipartResolver.setResolveLazily(this.multipartProperties.isResolveLazily()); 072 return multipartResolver; 073 } 074 075}