zoukankan      html  css  js  c++  java
  • 从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.

  • 相关阅读:
    Android应用程序窗口(Activity)实现框架简要介绍和学习计划
    Android系统Surface机制的SurfaceFlinger服务简要介绍和学习计划
    《Android系统源代码情景分析》一书勘误
    Android应用程序窗口(Activity)的运行上下文环境(Context)的创建过程分析
    非IE内核浏览器支持activex插件
    让火狐浏览器可以访问含有activex控件网页的三种方式
    C++中delete和delete[]的区别
    动态加载JS脚本
    创建一个弹出DIV窗口
    C# 向下遍历删除子目录和子文件 及 向上遍历空的父目录
  • 原文地址:https://www.cnblogs.com/adolfmc/p/3173352.html
Copyright © 2011-2022 走看看