转:web程序的初始化问题——ServletContextListener----调试成功

当web服务器启动后,它会去解析web.xml,如果我们需要在服务启动以后做一些初始化,那么可以在web.xml中配置ServeltContextListener来达到初始化,因为在Web应用程序的“初始阶段”,Servlet容器会调用ServletContextListener对象的contextInitialized()方法

   public class MyWebContextListener implements ServletContextListener{

// @Override
 public void contextDestroyed(ServletContextEvent event) {
  
 }

// @Override
 public void contextInitialized(ServletContextEvent event) {
  SpringConfig.init(event.getServletContext());
  DataManage.getInstance();
 }

}

然后将这个WebContextListener配到web.xml中即可

     <listener>
        <listener-class>com.MyWebContextListener</listener-class>
    </listener>

在初始化阶段,web容器(如tomcat)将会调用这个 contextInitialized方法,这时如果我们需要Spring的ApplicationContext对象时,就可以在init方法中实现:

 public class SpringConfig {
 private static ApplicationContext springContext;
 public static void init(ServletContext servletContext) {
  springContext=WebApplicationContextUtils.getWebApplicationContext(servletContext);
 }
 
 public static ApplicationContext getSpringContext(){
  return springContext;
 }
}

这样我们就可以通过单例模式得到的ApplicationContext对象springContext来随意地取得所需要的service bean了:

public class DataManage {
 private static DataManage instance;
 private TaskTypeService taskTypeSV=(TaskTypeService) SpringConfig.getSpringContext()
 .getBean("taskTypeService");
 private StageService stageSV=(StageService) SpringConfig.getSpringContext()
 .getBean("stageService");
 private ProjectService projectSV=(ProjectService) SpringConfig.getSpringContext()
 .getBean("projectService");
// private ProjectTypeService projectTypeSV;
 private UserService userSV=(UserService) SpringConfig.getSpringContext()
 .getBean("userService");
 private CategoryService categorySV=(CategoryService) SpringConfig.getSpringContext()
 .getBean("categoryService");

 

web程序的初始化问题——ServletContextListener----调试成功

web程序的初始化问题——ServletContextListener
时间: 2005-01-04

应用ServletContextListener接口,可以实现在web应用程序初始化时,自动运行一些初始化程序。

ServletContextListener接口定义的方法

方法名称

调用时机

Void contextInitialized(ServletContextEvent sce)

Web应用程序的“初始阶段”,Servlet容器会调用ServletContextListener对象的contextInitialized()方法

Void contextDestroyed(ServletContextEvent sce)

Web应用程序的“结束阶段”,Servlet容器会调用ServletContextListener对象的contextDestoryed()方法

应用此接口时,要在web.xml文件内定义“监听器类”的名称,此时要注意:

在Servlet规范中并未限制一个Web应用程序只能对应一个“监听器类”,但是在web.xml内定义<listener>元素时得注意下列两点:
<listener>元素必须出现在任何Context起始参数(由<context-param>元素所定义)之后。
<listener>元素必须出现在任何Servlet实体(由<servlet>元素所定义)之前。

举例:

web.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-app_2_4.xsd"
    version="2.4">

    <description>
      test servlet listener
    </description>
    <display-name>testServletListener</display-name>
 <listener>
  <listener-class>com.chuyang.Test
   </listener-class>
</listener>
 
</web-app>

Test.java:

package com.chuyang;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class Test implements ServletContextListener {
 public void contextInitialized(ServletContextEvent event) {
  System.out.println("servlet initialized.........");
 }
 public void contextDestroyed(ServletContextEvent event) {
  System.out.println("servlet destroyed..........");
 }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值