Defined by Java configuration

In this chapter, instead of the XML configuration, we will use Java to configure DispatcherServlet in the servlet container for our web application. Servlet 3.0 and later supports java-based bootstrapping, so, we can avoid using the web.xml file. Instead of this, we can create a java class that implements the javax.servlet.ServletContainerInitializer interface. Spring MVC provides the WebApplicationInitializer interface to ensure that your spring configuration is loaded and initialized in any Servlet 3 container. But the Spring MVC framework makes it even easier by providing an abstract class implementation of the WebApplicationInitializer interface. By using this abstract class, you just map your servlet mapping, and provide the root and MVC configuration classes. I, personally, prefer this way of configuration in my web application. The following is the code for this configuration class:

    package com.packt.patterninspring.chapter10.bankapp.web; 
 
    import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; 
 
    import com.packt.patterninspring.chapter10.bankapp.config.AppConfig; 
    import com.packt.patterninspring.chapter10.bankapp.web.mvc.SpringMvcConfig; 
 
    public class SpringApplicationInitilizer extends AbstractAnnotationConfigDispatcherServletInitializer
{ // Tell Spring what to use for the Root context: as ApplicationContext - "Root" configuration @Override protected Class<?>[] getRootConfigClasses() { return new Class <?>[]{AppConfig.class}; } // Tell Spring what to use for the DispatcherServlet context: WebApplicationContext- MVC
configuration @Override protected Class<?>[] getServletConfigClasses() { return new Class <?>[]{SpringMvcConfig.class}; } // DispatcherServlet mapping, this method responsible for URL pattern as like in web.xml file
<url-pattern>/</url-pattern> @Override protected String[] getServletMappings() { return new String[]{"/"}; } }

As seen in the preceding code, the SpringApplicationInitializer class extends the AbstractAnnotationConfigDispatcherServletInitializer class. It asks only the required information from the developer, and all configurations related to the DispatcherServlet are configured by this class using the servlet container interfaces. Take a look at the following diagram to understand more about the AbstractAnnotationConfigDispatcherServletInitializer class and its implementation to configure the DispatcherServlet in the application:

You have seen that the SpringApplicationInitilizer class overrides three methods of the AbstractAnnotationConfigDispatcherServletInitializer class, that is, getServletMappings(), getServletConfigClasses(), and getRootConfigClasses(). The method getServletMappings() defines the servlet mapping-in our application, it's mapped to "/". The method getServletConfigClasses() asks DispatcherServlet to load its application context with the beans defined in the SpringMvcConfig configuration class. This configuration file has bean definitions related to the web components such as controllers, view resolvers, and handler mappings. A Spring web application has another application context, and it is created by ContextLoaderListener. So, another method, getRootConfigClasses(), loads the other beans such as services, repositories, data-source, and other application beans typically required in the middle-tier and data-tier of the application defined in the AppConfig configuration class.

The Spring Framework provides a listener class--ContextLoaderListener. It is responsible for bootstrapping the backend application context.

Let's see the following diagram to understand more about the Spring web application design after starting up the servlet container:

As you can see in the last diagram, the web component beans definitions configuration classes returned by the getServletConfigClasses() method are loaded by the DispatcherServlet, and the other application beans definition configuration classes returned by the getRootConfigClasses() method are loaded by the ContextLoaderListener.

A Java-based web configuration will only work when deploying to a server that supports Servlet 3.0, such as Apache Tomcat 7 or higher.

Let's see how to enable more features of the Spring MVC Framework in the coming section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset