DispatcherServlet Java Based Configuration





import org.springframework.web.servlet.support.
AbstractAnnotationConfigDispatcherServletInitializer;
public class MyWebAppInit
extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[] { RootDataConfig.class };
    }
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[] { AppConfig.class };
    }
}

Above configuration works in the container which support servlet3.0

(any) MyWebAppInit class extends AbstractAnnotationConfigDispatcherServletInitializer.
So, it automatically be used to configure the DispatcherServlet and the Spring's application context in the
servlet context of the web application.

When DispatcherServlet starts
-Create Spring Application Context
-Load beans into Application Context from configuration file or configuration classes.
-In Above example it loads bean into Application context from the AppConfig class provided in the getServletConfigClasses() method.
-Dispatcher is responsible for loading bean regarding web components (Controllers, ViewResolvers, Handlers etc)
-ContextLoaderListener creates another application context and it is responsible for creating rest of the beans like those
beans which are responsible for middle and data layer backend core functionality.

-AbstractAnnotationConfigDispatcherServletInitializer is responsible to create DispatcherServlet and ContextLoaderListener.
-@COnfiguration classes returned from the getServletConfigClasses() method will provide beans to configure application context created by the DispatcherServlet
-@Configuration classes returned from then getRootConfigClasses() method will provide beans to configure application context created by the  ContextLoaderListener