34 lines
1.4 KiB
Java
34 lines
1.4 KiB
Java
package com.baeldung.web;
|
|
|
|
import java.util.Set;
|
|
|
|
import javax.servlet.ServletContext;
|
|
import javax.servlet.ServletException;
|
|
import javax.servlet.ServletRegistration;
|
|
|
|
import org.springframework.web.WebApplicationInitializer;
|
|
import org.springframework.web.context.ContextLoaderListener;
|
|
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
|
import org.springframework.web.context.support.GenericWebApplicationContext;
|
|
import org.springframework.web.servlet.DispatcherServlet;
|
|
|
|
public class WebAppInitializer implements WebApplicationInitializer {
|
|
|
|
@Override
|
|
public void onStartup(final ServletContext sc) throws ServletException {
|
|
|
|
final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
|
|
root.scan("com.baeldung.spring");
|
|
|
|
sc.addListener(new ContextLoaderListener(root));
|
|
|
|
final ServletRegistration.Dynamic appServlet = sc.addServlet("spring", new DispatcherServlet(new GenericWebApplicationContext()));
|
|
appServlet.setLoadOnStartup(1);
|
|
|
|
final Set<String> mappingConflicts = appServlet.addMapping("/");
|
|
if (!mappingConflicts.isEmpty()) {
|
|
throw new IllegalStateException("'appServlet' could not be mapped to '/' due " + "to an existing mapping. This is a known issue under Tomcat versions " + "<= 7.0.14; see https://issues.apache.org/bugzilla/show_bug.cgi?id=51278");
|
|
}
|
|
}
|
|
}
|