需要做一个参数初始化类,当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.

从servlet中获取spring的WebApplicationContext的更多相关文章

  1. 在Servlet中获取spring容器WebApplicationContext

    WebApplicationContext springContext = WebApplicationContextUtils.getRequiredWebApplicationContext(ge ...

  2. Servlet中获取Spring管理的bean

    描述: 在Servlet中调用Spring管理的接口,可以使Dao/Service/ServiceImpl. 前提是在调用的bean中有注解: @Repository("beanName&q ...

  3. 在Servlet中获取Spring注解的bean

    最近由于项目中出现了Servlet调用Spring的bean,由于整个项目中所有的bean均是注解方式完成,如@Service,@Repository,@Resource等,但是Spring的容器管理 ...

  4. 如何在servlet中获取spring创建的bean

    package com.yxf.controller; import java.io.IOException; import javax.servlet.ServletException; impor ...

  5. web项目中获取spring的bean对象

    Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架,如何在程序中不通过注解的形式(@Resource.@Autowired)获取Spring配置的bean呢? Bean工厂(c ...

  6. Spring管理Filter和Servlet(在servlet中注入spring容器中的bean)

    在使用spring容器的web应用中,业务对象间的依赖关系都可以用context.xml文件来配置,并且由spring容器来负责依赖对象 的创建.如果要在servlet中使用spring容器管理业务对 ...

  7. Web版需求征集系统所得1,servlet中获取checkbox复选框的值

    servlet中获取checkbox复选框的值 </tr> <tr> <td align="right">研究类型</td> < ...

  8. jsp内置对象pageContext如何在Servlet中获取值

    pageContext javax.servlet.jsp.PageContext 的实例,该对象代表该JSP 页面上下文,使用该对象可以访问页面中的共享数据.常用的方法有getServletCont ...

  9. 在自定义组件中获取spring底层组件

    要想在自定义组件中获取spring底层的各种组件,只需让自定义组件实现一系列接口即可,这些接口都是Aware的子接口.常见的有: 1. ApplicationContextAware——用于获取IOC ...

随机推荐

  1. vijos p1882 智力题

    题意: 清晨, Alice与Bob在石阶上玩砖块.他们每人都有属于自己的一堆砖块.每人的砖块都由N列组成且N是奇数.Alice的第i列砖块有m[i]个.而Bob的第i列砖块有s[i]个. 他们想建造城 ...

  2. 2、Redis的基础知识

     写在前面的话:读书破万卷,编码如有神 -------------------------------------------------------------------- 主要内容包括: red ...

  3. ASP.NET 构建高性能网站 第4篇

    部署优化 我们都知道,不同的部署方式对站点的性能是有影响的,可能有些朋友已经知道了这点,不管怎样,我们这里还是详细系统的讲述一下这个问题,熟悉的朋友权当回顾J. Release方式编译项目 如果我们的 ...

  4. request.setAttribute()、session.setAttribute()和request.getParameter()的联系与区别(记录)

    1.session.setAttribute()和session.getAttribute()配对使用,作用域是整个会话期间,在所有的页面都使用这些数据的时候使用. 2.request.setAttr ...

  5. Go语言Web框架gwk介绍 (五)

    Session Go的net/http本身不带session的机制,需要开发人员自行实现,gwk实现了内存中的session存储机制,如果需要将session存在其他地方比如redis或者memcac ...

  6. python中的__all__和__slots__

    python两个有趣属性__all__可用于模块导入时限制,如:from module import *此时被导入模块若定义了__all__属性,则只有all内指定的属性.方法.类可被导入~若没定义, ...

  7. Windows操作系统下的MySQL主从复制及读写分离

    一.主服务器(master)配置 1.修改MySQL配置文件my.ini [mysqld] log-bin=mysql-binlog-bin-index=mysql-bin.indexserver-i ...

  8. 使用Bootstrap 3开发响应式网站实践03,轮播下方的内容排版

    通常把一些重要信息.需要重点标注的信息放在轮播的下方显示,这部分区域用到了大字体的标题.副标题以及段落文字等. <div class="row" id="bigCa ...

  9. .NET:CLR via C# Primitive Thread Synchronization Constructs

    User-Mode Constructs The CLR guarantees that reads and writes to variables of the following data typ ...

  10. javascript和python取dict字典对象的不同

    dict1={"a":1,"b":2,"22a":44} JS: dict1.a 和 dict1["a"]都可以 pyt ...