Different Ways of Bootstrapping the Spring Framework..

.. in Web Applications!

Bootstrap in Deployment Descriptor - Configuration in XML!

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/rootContext.xml</param-value>
</context-param> 
 
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
 
<servlet>
    <servlet-name>springDispatcher</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/servletContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup> 
</servlet>
 
<servlet-mapping> 
    <servlet-name>springDispatcher</servlet-name> 
    <url-pattern>/</url-pattern>
</servlet-mapping>

Bootstrap in Deployment Description - Configuration in Java!

<context-param>
    <param-name>contextClass</param-name>
    <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
 
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>biz.tugay.MyRootContextConfiguration</param-value>
</context-param>
 
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
 
<servlet>
    <servlet-name>springDispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </init-param>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>biz.tugay.config.ServletContextConfiguration</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
 
<servlet-mapping>
    <servlet-name>springDispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

Bootstrap in Java - Configuration in XML!

public class Bootstrap implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext container) {
        XmlWebApplicationContext rootContext = new XmlWebApplicationContext();
        rootContext.setConfigLocation("/WEB-INF/rootContext.xml");
        container.addListener(new ContextLoaderListener(rootContext));
        XmlWebApplicationContext servletContext = new XmlWebApplicationContext();
        servletContext.setConfigLocation("/WEB-INF/servletContext.xml");
        ServletRegistration.Dynamic dispatcher = container.addServlet("springDispatcher", new DispatcherServlet(servletContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
    }
}

Bootstrap in Java - Configuration in Java!

public class Bootstrap implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext container) {
        AnnotationConfigWebApplicationContext rootContext 
            = new AnnotationConfigWebApplicationContext();
        rootContext.register(com.wrox.config.RootContextConfiguration.class);
        container.addListener(new ContextLoaderListener(rootContext));
 
        AnnotationConfigWebApplicationContext servletContext 
            = new AnnotationConfigWebApplicationContext();
        servletContext.register(biz.tugay.config.ServletContextConfiguration .class);
 
        ServletRegistration.Dynamic dispatcher 
        = container.addServlet("springDispatcher", new DispatcherServlet(servletContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
    }
}

Bootstrap in Java - Configuration in Java - 2!

public class MyWebAppInnit extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[]{RootConfigClass.class};
    }
 
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[]{ServletConfigClass.class};
    }
 
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
}