从servlet中获取spring的WebApplicationContext

本文介绍如何使用Spring框架在Web应用启动时从数据库加载参数并存入application域,确保全局可用。通过配置DispatcherServlet、ContextLoaderServlet及自定义InitialServlet实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需要做一个参数初始化类,当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里
 * @author 蔡科
 * 创建日期:2006-1-9
 
*/
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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值