从servlet中获取spring的WebApplicationContext

需要做一个参数初始化类,当web应用被加载时从数据库里取出相关的参数设置

,并把这些参数放置到application里,jsp页面可以从中取出。

1.在web.xml中配置:

<servlet>
        <servlet-name>Dispatcher</servlet-name>
        <servlet-

class>org.springframework.web.servlet.DispatcherServlet</servlet-

class
>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/Dispatcher-

servlet.xml,/WEB-INF/applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet>
        <servlet-name>context</servlet-name>
        <servlet-

class>org.springframework.web.context.ContextLoaderServlet</servlet-

class
>
        <load-on-startup>2</load-on-startup>
    </servlet>

    <servlet>
        <servlet-name>InitialServlet</servlet-name>
        <servlet-

class>com.anylinks.billreturn.Web.InitialServlet</servlet-class>
        <load-on-startup>3</load-on-startup>
    </servlet>


2.servlet代码

package com.anylinks.billreturn.Web;

import java.util.Collection;
import java.util.Iterator;

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.anylinks.billreturn.BO.SysParameter;
import com.anylinks.billreturn.Service.ISysParameterService;

/*
 * 初始化Servlet,从数据库中读取参数表,保存在application里
 
*/
public class InitialServlet extends HttpServlet {

    private Log log = LogFactory.getLog(this.getClass());

    private ISysParameterService sysParameterService;

    /**
     * 从数据库中读取参数表,保存在application里
     *
     * 
@throws ServletException
     *             if an error occure
     
*/
    public void init() throws ServletException {

        log.debug("start to intitail ");
        // 获取WebApplicationContext
        ServletContext application = getServletContext();
        WebApplicationContext wac = WebApplicationContextUtils
                .getWebApplicationContext

(application);

        // 调用sysParameterService取出所有的系统参数
        sysParameterService = (ISysParameterService) wac
                .getBean("sysParameterService");

        Collection paras =

sysParameterService.findAllParameters();
        log.debug("sys parameters size:" + paras.size());

        // 把参数加到application里去
        for (Iterator iter = paras.iterator(); iter.hasNext

();) {
            SysParameter para = (SysParameter) iter.next

();

            application.setAttribute(para.getParaName(),

para.getParaValue());

            log.debug("initial parameter: key=" +

para.getParaName()
                    + ", value=" +

para.getParaValue());

        }
    }

}




需要注意的地方:
1.仅仅配置一个DispatcherServlet是不够的,我开始就是这样,然后再servlet

里面怎么取都取不到WebApplicationContext 。配置上

org.springframework.web.context.ContextLoaderServlet之后才能取的到

WebApplicationContext 。
2.注意一下<load-on-startup>3</load-on-startup>,因为用到spring的

hibernateDaoSupport,所以必须在spring加载完之后再加载InitialServlet.

posted on 2013-07-05 11:17  adolfmc  阅读(7325)  评论(0编辑  收藏  举报