CodeGym /Java Course /Module 5. Spring /Servlet Configuration

Servlet Configuration

Module 5. Spring
Level 8 , Lesson 3
Available

Applications can declare infrastructure beans that are needed to process requests. DispatcherServlet checks the WebApplicationContext for each specialized bean. If there are no suitable bean types, then the default types listed in the file DispatcherServlet.properties.

In most cases, MVC Config is the best starting point. It declares the required beans in Java or XML and provides a higher-level configuration callback API to configure them.

Spring Boot builds on the Java MVC configuration for configuring Spring MVC and provides many additional convenience options.

1.1.4. Servlet Configuration

In the Servlet 3.0+ environment, it is possible to configure the servlet container programmatically as an alternative to, or in combination with, a web.xml file. The following example registers DispatcherServlet:

Java
import org.springframework.web.WebApplicationInitializer;
public class MyWebApplicationInitializer implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext container) {
        XmlWebApplicationContext appContext = new XmlWebApplicationContext();
        appContext.setConfigLocation("/WEB-INF/spring/dispatcher-config.xml");
        ServletRegistration.Dynamic registration = container.addServlet("dispatcher", new DispatcherServlet(appContext));
        registration.setLoadOnStartup(1);
        registration.addMapping("/");
    }
}
Kotlin
import org.springframework.web.WebApplicationInitializer
class MyWebApplicationInitializer : WebApplicationInitializer {
    override fun onStartup(container: ServletContext) {
        val appContext = XmlWebApplicationContext()
        appContext.setConfigLocation("/WEB-INF/spring/dispatcher-config.xml")
        val registration = container.addServlet("dispatcher", DispatcherServlet(appContext))
        registration.setLoadOnStartup(1)
        registration.addMapping("/")
    }
}

WebApplicationInitializer is an interface provided by Spring MVC that allows you to ensure that your implementation is discovered and automatically used to initialize any Servlet 3 container. An abstract implementation of the WebApplicationInitializer base class called AbstractDispatcherServletInitializer further simplifies DispatcherServlet registration by overriding methods to set the servlet display and DispatcherServlet configuration location.

This is recommended for applications that use a Java-based Spring configuration, as shown in the following example:

Java
public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return null;
    }
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[] { MyWebConfig.class };
    }
    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }
}
Kotlin
class MyWebAppInitializer : AbstractAnnotationConfigDispatcherServletInitializer() {
    override fun getRootConfigClasses(): Array<Class<*>>? {
        return null
    }
    override fun getServletConfigClasses(): Array<Class<*>>? {
        return arrayOf(MyWebConfig::class.java)
    }
    override fun getServletMappings(): Array<String> {
        return arrayOf("/")
    }
}

If you are using an XML-based Spring configuration, you should extend directly from AbstractDispatcherServletInitializer, as shown in the following example:

Java
public class MyWebAppInitializer extends AbstractDispatcherServletInitializer {
    @Override
    protected WebApplicationContext createRootApplicationContext() {
        return null;
    }
    @Override
    protected WebApplicationContext createServletApplicationContext() {
        XmlWebApplicationContext cxt = new XmlWebApplicationContext();
        cxt.setConfigLocation("/WEB-INF/spring/dispatcher-config.xml");
        return cxt;
    }
    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }
}
Kotlin
class MyWebAppInitializer : AbstractDispatcherServletInitializer() {
    override fun createRootApplicationContext(): WebApplicationContext? {
        return null
    }
    override fun createServletApplicationContext(): WebApplicationContext {
        return XmlWebApplicationContext().apply {
            setConfigLocation("/WEB-INF/spring/dispatcher-config.xml")
        }
    }
    override fun getServletMappings(): Array<String> {
        return arrayOf("/")
    }
}

AbstractDispatcherServletInitializer also provides a convenient way to add Filter instances and automatically map them to DispatcherServlet, as shown in the following example:

Java
public class MyWebAppInitializer extends AbstractDispatcherServletInitializer {
    // ...
    @Override
    protected Filter[] getServletFilters() {
        return new Filter[] {
            new HiddenHttpMethodFilter(), new CharacterEncodingFilter() };
    }
}
Kotlin
class MyWebAppInitializer : AbstractDispatcherServletInitializer() {
    // ...
    override fun getServletFilters(): Array<Filter> {
        return arrayOf(HiddenHttpMethodFilter(), CharacterEncodingFilter())
    }
}

Each filter is added with a default name based on its specific type and is automatically mapped to the DispatcherServlet.

The isAsyncSupported protected method in the AbstractDispatcherServletInitializer provides a single place to enable async support in the DispatcherServlet and all filters mapped to it. By default, this flag is set to true.

Finally, if you need to further customize the DispatcherServlet itself, you can override the createDispatcherServlet method.

Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION