在ServletContextListener实现类中获取spring注入对象

来源:互联网 发布:电脑磁盘数据恢复 编辑:程序博客网 时间:2024/06/05 06:59

由于项目需要,需在ServletContextListener监听接口实现类中调用spring注入的对象,以获取系统初始化参数.代码如下:

 

[java] view plain copy
  1. import java.io.IOException;  
  2. import java.util.List;  
  3. import javax.servlet.ServletContextEvent;  
  4. import javax.servlet.ServletContextListener;  
  5. import org.springframework.web.context.WebApplicationContext;  
  6. import org.springframework.web.context.support.WebApplicationContextUtils;  
  7. import pams.model.Device;  
  8. import pams.service.Impl.DeviceManage;  
  9. import pams.socket.TcpManager;  
  10. import pams.socket.TcpServer;  
  11. /** 
  12.  * 系统初始化 
  13.  * @author 恶灵骑士 
  14.  * 
  15.  */  
  16. public class SysInitServlet implements ServletContextListener {  
  17.  //获取spring注入的bean对象  
  18.  private WebApplicationContext springContext;  
  19.  private DeviceManage deviceManager;  
  20.    
  21.  //数据采集仪服务线程  
  22.  TcpServer daqServer = null;  
  23.  //继电器服务线程  
  24.  TcpServer realyServer = null;  
  25.  //Tcp连接管理器  
  26.  Thread tcpManager = null;  
  27.    
  28.  public SysInitServlet(){  
  29.   super();  
  30.  }  
  31.  /** 
  32.   *应用程序退出时调用 
  33.   */  
  34.  @Override  
  35.  public void contextDestroyed(ServletContextEvent event) {  
  36.   serverDestroyed();  
  37.   System.out.println("应用程序关闭!");  
  38.  }  
  39.    
  40.  /** 
  41.   * 应用程序加载时调用 
  42.   */  
  43.  @Override  
  44.  public void contextInitialized(ServletContextEvent event) {  
  45.   springContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());  
  46.   if(springContext != null){  
  47.    deviceManager = (DeviceManage)springContext.getBean("deviceManage");  
  48.   }else{  
  49.    System.out.println("获取应用程序上下文失败!");  
  50.    return;  
  51.   }  
  52.   System.out.println("初始化系统服务!");  
  53.   serverInitialized();  
  54.   System.out.println("TcpServer初始化完成!");  
  55.  }  
  56.    
  57.  /** 
  58.   * 系统服务初始化 
  59.   * device_type:由于只需要知道采集仪,继电器端口号 
  60.   * 所以device_type设置为0 
  61.   */  
  62.  public void serverInitialized(){  
  63.        if(deviceManager == null){  
  64.             System.out.println("获取设备管理器失败!");  
  65.          return;  
  66.       }  
  67.       List<Device> devices = this.deviceManager.load(0);  
  68.      for(Device device : devices){  
  69.           System.out.println("设备ID-->"+device.getId());  
  70.           System.out.println("采集仪端口号-->"+device.getProperty1());  
  71.           System.out.println("继电器端口号-->"+device.getProperty2());  
  72.           System.out.println("所属大棚-->"+device.getShed().getName());  
  73.     }  
  74.      //开启串口服务器-数据采集仪监听线程  
  75.      TcpServer daqServer = new TcpServer(5678);  
  76.      TcpServer realyServer = new TcpServer(5679);  
  77.     tcpManager = new Thread(new TcpManager());  
  78.     if(daqServer.getServer() != null)  
  79.     {  
  80.         new Thread(daqServer).start();  
  81.     }  
  82.     if(realyServer.getServer()!=null){  
  83.         new Thread(realyServer).start();  
  84.    }  
  85.     tcpManager.start();  
  86.  }  
  87.    
  88.  /** 
  89.   * 系统服务注销 
  90.   */  
  91.  @SuppressWarnings("deprecation")  
  92.  public void serverDestroyed(){  
  93.     
  94.   if(daqServer!=null){  
  95.    try {  
  96.     daqServer.getServer().close();  
  97.    } catch (IOException e) {  
  98.     System.out.println("数据采集仪服务线程关闭出错 --> "+e.getMessage());  
  99.    }  
  100.   }  
  101.   if(realyServer!=null){  
  102.    try {  
  103.     realyServer.getServer().close();  
  104.    } catch (IOException e) {  
  105.     System.out.println("继电器服务线程关闭出错 --> "+e.getMessage());  
  106.    }  
  107.   }  
  108.   if(tcpManager!=null){  
  109.    tcpManager.stop();  
  110.   }  
  111.  }  
  112. }  

springContext为spring管理的应用程序上下文,里面存储spring管理的各种bean对象.deviceManager就是通过spring注入的设备控制业务层.

注意事项:

1.  由于实现的是ServletContextListener接口,故需要实现public void contextInitialized(ServletContextEvent event)方法和public void contextDestroyed(ServletContextEvent event)方法.前者在应用程序加载时调用,里面添加一些初始化业务.如初始化springContext,调用serverInitialized()完成系统服务初始化,后者用于应用程序关闭时调用,主要完成服务资源的注销.

既然是listener接口就要在web.xml中配置相关参数,如下:

<listener>
   <listener-class>pams.servlet.SysInitServlet</listener-class>
  </listener>

2.  由于需获取spring管理bean,故该listner配置需要放在spring监听器配置之后,以使spring完成初始化,如下:

 

[html] view plain copy
  1. <!--Spring ApplicationContext载入-->  
  2.  <listener>  
  3.      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  4.  </listener>  
  5.  <!-- Spring ApplicationContext配置文件的路径,此参数用于后面的Spring-Contextloader -->  
  6.  <context-param>  
  7.      <param-name>contextConfigLocation</param-name>  
  8.      <param-value>classpath:beans.xml</param-value>  
  9.  </context-param>  
  10. lt;!-- 系统服务初始化 -->  
  11.  <listener>  
  12.   <listener-class>pams.servlet.SysInitServlet</listener-class>  
  13.  </listener>  

如果想在外部类中调用通过这种方法得到的spring对象,可以把springContext设为static,然后提供相应的get方法,此处由于不需要故设为private..

阅读全文
0 0
原创粉丝点击