Sunday, June 24, 2012

Web application context (spring load)

Following is an example of how spring beans can be loaded from a servlet. 

The web.xml configuration required is as follows. The spring bean definition xml file path should be given as a context parameter. Spring's ContextLoaderListener has to be defined as a listener in the web.xml. The sub-element 'load-on-startup' should be present in the servlet definition for the servlet to load at the web application is deploy time (If not the servlet will be initialized upon the first request to it).

 ==================================================================
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-beans.xml</param-value>
    </context-param>
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/log4j.xml</param-value>
        <description>location of log4j configuration file, used by log4jconfiglistener</description>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>InitServlet</servlet-name>
        <servlet-class>com.shyarmal.servlet.InitServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
   
    <servlet-mapping>
        <servlet-name>InitServlet</servlet-name>
        <url-pattern>/init</url-pattern>
    </servlet-mapping>

</web-app>
 ==================================================================

The servlet code is below. The method, init(ServletConfig) [or init()] is overridden to load the web application context with, "WebApplicationContextUtils .getWebApplicationContext(config.getServletContext());". Servlet's init is called when the same is initialized.

 ==================================================================

package com.shyarmal.servlet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;



public class InitServlet extends HttpServlet {

    private static final Logger LOGGER = LoggerFactory.getLogger(InitServlet.class);

    @Override
    public void init(ServletConfig config) throws ServletException {
        try {
            LOGGER.debug("initializing email service.");
            ApplicationContext ac = WebApplicationContextUtils              .getWebApplicationContext(config.getServletContext());
            LOGGER.info("Started email service.");
        } catch (Exception e) {
            LOGGER.error("Failed to load application context.", e);
        }
        super.init(config);
    }
}

 ==================================================================


thanks,
Shyarmal.


No comments:

Post a Comment