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.

  • 相关阅读:
    概率论02 概率公理-集合
    概率论1 计数-排列-组合
    matplotlib 练习
    python itertools模块练习
    主观世界的破碎与重建——湖畔大学的失败课外课
    Python操作MongoDB(PyMongo模块的使用)
    python操作json数据格式--基础
    linux shell awk实现实时监控网卡流量脚本
    Python 的十个自然语言处理工具
    实现优先级队列 --heapq模块
  • 原文地址:https://www.cnblogs.com/adolfmc/p/3173352.html
Copyright © 2011-2022 走看看