`
haofenglemon
  • 浏览: 242989 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

servlet中获取spring的WebApplicationContext

    博客分类:
  • ssh
阅读更多
SSH 整合中的监听器的问题

在做项目时,由于有一部分数据经常被用做查询,为了降低应用程序访问数据库的时间,
想在web   server   启动时,设置一个监听器,将查询的结果集放在application中.

监听器中的dao对象是由spring注入的,问题出在tomcat   启动时,先启动监听,再启动
spring   容器并注入相应的bean   ,所以启动监听时访问到的dao   对象是null,这时会报一个listener   error   错误,   使得web   server   无法正常启动.





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

        }
    }

}
分享到:
评论

相关推荐

    在Servlet直接获取Spring框架中的Bean.docx

    以下是如何在Servlet中直接获取Spring框架中的Bean的方法。 首先,我们理解Spring容器,即ApplicationContext,它是管理Bean的核心组件。它负责读取配置文件(如XML或Java配置),创建并初始化Bean,以及维护它们...

    Spring获取webapplicationcontext,applicationcontext几种方法详解

    在 Spring 框架中,获取 WebApplicationContext 和 ApplicationContext 对象是非常重要的,因为它们提供了访问 Spring 容器中的 Bean 对象的入口。下面将详细介绍五种获取 WebApplicationContext 和 ...

    Spring的注入在Servlet中使用

    Spring的注入在Servlet中使用:在Servlet中使用Spring注入的信息,需要WebApplicationContext这个专门为Web准备的应用上下文

    Spring 管理filter 和servlet

    这些代理类负责从Spring的WebApplicationContext中获取实际的Filter或Servlet实例,并将请求委托给它们。 3. **配置WebApplicationContext的初始化** 在`web.xml`中,使用`ContextLoaderListener`来初始化Spring...

    JSP 开发之servlet中调用注入spring管理的dao

    具体到我们的场景,如果DAO对象被Spring管理,而Servlet不在Spring容器中,那么我们就需要在Servlet初始化的时候从Spring容器中获取DAO对象。 具体操作步骤如下: 1. 在Servlet中定义一个私有成员变量作为DAO对象...

    Web服务启动时自动加载Servlet,并读取数据库内容

    这里,我们使用`WebApplicationContextUtils`类来从Servlet上下文中获取Spring的WebApplicationContext,然后通过ApplicationContext获取Service层的bean。 下面是一个示例,展示了如何在Servlet中获取Service层的...

    几种spring获取bean的方法.txt

    根据提供的文件信息,我们可以总结出以下关于Spring框架中获取Bean的几种方法的相关知识点: ### Spring框架简介 Spring框架是一款开源的轻量级Java EE应用程序开发框架,它通过提供一系列强大的功能来简化Java...

    Web项目中使用Spring, 使用 Spring 的器监听器 ContextLoaderListener.docx

    在Servlet中,可以通过`WebApplicationContext`接口的`getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)`方法获取到Spring的根应用上下文,从而得到Service对象...

    spring中的所有配置

    可以通过`WebApplicationContextUtils`类从`ServletContext`中获取`WebApplicationContext`实例: ```java WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(this....

    Spring整合Struts

    另一种整合方式是通过继承Spring的ActionSupport类,这使得开发者能够显式地使用getWebApplicationContext()方法获取Spring管理的bean。这种方式更适用于那些需要深度集成Spring特性的场景,比如AOP或更复杂的依赖...

    Spring中ApplicationContext加载机制

    配置完成之后,即可通过 WebApplicationContextUtils.getWebApplicationContext 方法在 Web 应用中获取 ApplicationContext 引用。例如: ```java ApplicationContext ctx = WebApplicationContextUtils.get...

    获得spring里注册Bean的四种方法

    首先,我们需要在类中定义一个 WebApplicationContext 变量,以便获取应用程序的上下文环境变量。然后,我们可以使用 ctx.getBean() 方法来获取指定名称的 Bean 对象。 ```java public class BaseDispatchAction ...

    在Eclipse 中创建Spring的 Web应用.doc

    在Web应用中,Spring提供了`org.springframework.web.context.ContextLoader`类来加载`WebApplicationContext`。有两种主要的加载方式: - **ContextLoaderListener**:这是一个Servlet监听器,它会在Web应用启动...

    Spring MVC之WebApplicationContext_动力节点Java学院整理

    WebApplicationContext是针对Web应用而设计的,它会持有对ServletContext的引用,允许从Web相关的对象(如Servlet)中获取Bean。WebApplicationContext通常在Web应用启动时初始化,并在Web应用运行期间一直存在,...

    Spring Web MVC外文翻译

    Spring Web MVC 是基于 Servlet API 构建的原始 Web 框架,它从 Spring 框架诞生之初就被包含其中。正式名称“Spring Web MVC”来源于其源模块 `spring-webmvc`,但在实际使用中更常见地被称为“Spring MVC”。 与 ...

    SpringTest_springtest_spring_java_Framework_

    Spring Boot Test则为Spring Boot应用提供了更便捷的测试工具,包括对MockMVC、WebApplicationContext和嵌入式Servlet容器的支持。 "spring"和"java Framework"明确了讨论的焦点是基于Java的Spring框架。Spring框架...

    JSP 获取spring容器中bean的两种方法总结

    下面将详细介绍在JSP页面中获取Spring容器中bean的两种方法。 ### 方法一:在Web应用中使用 在Web应用中,一般推荐使用Spring提供的WebApplicationContextUtils工具类来获取ApplicationContext。这种方法主要适用...

    spring在web.xml中和在struts中的不同配置.[收集].pdf

    ServletContext也是容器,存储了Web应用的各种属性,包括Spring的WebApplicationContext,这样其他Servlet和Filter就能通过ServletContext获取到ApplicationContext,进而访问和操作Bean。 在Struts中,Spring的...

    JSP 获取Spring 注入对象示例

    这样,我们就成功地在JSP页面中获取到了Spring管理的`MyService`对象,可以进一步调用其方法来执行业务逻辑。 需要注意的是,虽然在JSP中直接获取Spring bean是可行的,但这并不推荐。因为JSP的主要职责是呈现视图...

    struts 整合spring 例子,测试通过

    这样,Action可以从Spring容器中获取依赖,实现依赖注入。 1. **配置Struts-Spring插件**。在Struts2的配置文件(struts.xml)中,需要启用Spring插件并指定Action类的Spring bean名称。 ```xml ...

Global site tag (gtag.js) - Google Analytics