ServletContextListener使用详解以及web容器中获取spring容器

 ServletContextListener 接口是servlet的一个接口,它能够监听 ServletContext 对象的生命周期,实际上就是监听 Web 应用的生命周期(Tomcat的启动与关闭)。

服务器启动时,ServletContextListener 的 contextInitialized()方法被调用,服务器将要关闭时,ServletContextListener 的 contextDestroyed()方法被调用,用 ServletContext.setAttribute()方法将缓存类保存在 ServletContext 的实例中,用 ServletContext.getAttribute()读取ServletContext中的内容。

具体使用示例:

第一步:public class MyInitDataListener implements ServletContextListener{

  /**

  1.  * 当Servlet 容器启动Web 应用时调用该方法
  2.   * 并且对那些在Web 应用启动时就需要被初始化的Servlet 进行初始化。 
  3.   */  
  4. contextInitialized(ServletContextEvent sce)  
  5.  /** 
  6.   * 当Servlet 容器终止Web 应用时调用该方法。
  7.   */  
  8.  contextDestroyed(ServletContextEvent sce)

}

第二步:部署ServletContextListener

实现了ServletContextListener 编译后,把它放在正确的WEB-INF/classes目录下,更改WEB-INF目录下的 web.xml文件,在web-app节点里添加:

<listener>
    <listener-class>MyInitDataListener类的全路径</listener-class>
</listener>
ApplicationContext是Spring的核心,Context我们通常解释为上下文环境,我想用“容器”来表述它更容易理解一些,ApplicationContext则是“应用的容器”了Spring把Bean放在这个容器中,在需要的时候,用getBean方法取出,

在Web应用中,我们会用到WebApplicationContext,WebApplicationContext继承自ApplicationContext,

Spring把WebApplicationContext(XmlWebApplicationContext是默认实现类)放在了ServletContext中,ServletContext也是一个“容器”,也是一个类似Map的结构,而WebApplicationContext在ServletContext中的KEY就是WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,我们如果要使用WebApplicationContext则需要从ServletContext取出,Spring提供了一个WebApplicationContextUtils类,可以方便的取出WebApplicationContext,只要把ServletContext传入就可以了。

方法一:

ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml");
ac.getBean("beanId");

说明:这种方式适用于采用Spring框架的独立应用程序,需要程序通过配置文件手工初始化Spring的情况。

方法二:通过Spring提供的工具类获取ApplicationContext对象

ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);
ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);
ac1.getBean("beanId");
ac2.getBean("beanId");

说明:这种方式适合于采用Spring框架的B/S系统,通过ServletContext对象获取ApplicationContext对象,然后在通过它获取需要的类实例。

上面两个工具方式的区别是,前者在获取失败时抛出异常,后者返回null。

其中 servletContext sc 可以具体 换成 servlet.getServletContext()或者 this.getServletContext() 或者 request.getSession().getServletContext(); 另外,由于spring是注入的对象放在ServletContext中的,所以可以直接在ServletContext取出 WebApplicationContext 对象: WebApplicationContext webApplicationContext = (WebApplicationContext) servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

web容器中获取spring容器实例:

1.在application.xml中配置:

<bean name="schoolParamBean" class="com.csn.my.config.ParamInfo">

        <property name="schoolCode" value="000001"/>
 <property name="dataURL" value="http://www.aa.com/E/Service/" />
        <property name="Count" value="1000" />
        <property name="Days" value="15" />
    </bean>

2.创建实体类实现ServletContextListener

public class MyInitDataListener implements ServletContextListener{
     @Override
     public void contextInitialized(ServletContextEvent  arg0) {
        
          ServletContext servletContext = arg0.getServletContext();
                 
           WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
          SchoolParam param = (SchoolParam) ctx.getBean("schoolParamBean");

          aa=param.getSchoolCode();
     }

}






  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值