BAEL-86: Refactored WebInitializer and added MultipartResolver.

This commit is contained in:
Christian Rädel 2016-10-19 01:56:47 +02:00 committed by slavisa-baeldung
parent 88a4f61917
commit a1a8c29c63
3 changed files with 37 additions and 28 deletions

View File

@ -0,0 +1,30 @@
package com.baeldung.spring.dispatcher.servlet;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
public class WebApplicationInitializer implements org.springframework.web.WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext rootContext =
new AnnotationConfigWebApplicationContext();
rootContext.register(RootConfiguration.class);
servletContext.addListener(new ContextLoaderListener(rootContext));
AnnotationConfigWebApplicationContext webContext =
new AnnotationConfigWebApplicationContext();
webContext.register(WebConfiguration.class);
webContext.scan("com.baeldung.spring.dispatcher.servlet.web");
ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher",
new DispatcherServlet(webContext));
servlet.addMapping("/*");
MultipartConfigElement multipartConfigElement =
new MultipartConfigElement("/tmp");
servlet.setMultipartConfig(multipartConfigElement);
}
}

View File

@ -6,6 +6,8 @@ import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.ui.context.support.ResourceBundleThemeSource;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.ThemeResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@ -111,4 +113,9 @@ public class WebConfiguration extends WebMvcConfigurerAdapter {
themeChangeInterceptor.setParamName("theme");
return themeChangeInterceptor;
}
@Bean
public MultipartResolver multipartResolver() {
return new StandardServletMultipartResolver();
}
}

View File

@ -1,28 +0,0 @@
package com.baeldung.spring.dispatcher.servlet;
import com.baeldung.spring.dispatcher.servlet.web.filters.RequestLoggingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import javax.servlet.Filter;
public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[]{RootConfiguration.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[]{WebConfiguration.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/*"};
}
@Override
protected Filter[] getServletFilters() {
return new Filter[]{new RequestLoggingFilter()};
}
}