由于項目需要,需在ServletContextListener監聽接口實作類中調用spring注入的對象,以擷取系統初始化參數.代碼如下:
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import pams.model.Device;
import pams.service.Impl.DeviceManage;
import pams.socket.TcpManager;
import pams.socket.TcpServer;
/**
* 系統初始化
* @author 惡靈騎士
*
*/
public class SysInitServlet implements ServletContextListener {
//擷取spring注入的bean對象
private WebApplicationContext springContext;
private DeviceManage deviceManager;
//資料采集儀服務線程
TcpServer daqServer = null;
//繼電器服務線程
TcpServer realyServer = null;
//Tcp連接配接管理器
Thread tcpManager = null;
public SysInitServlet(){
super();
}
/**
*應用程式退出時調用
*/
@Override
public void contextDestroyed(ServletContextEvent event) {
serverDestroyed();
System.out.println("應用程式關閉!");
}
/**
* 應用程式加載時調用
*/
@Override
public void contextInitialized(ServletContextEvent event) {
springContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
if(springContext != null){
deviceManager = (DeviceManage)springContext.getBean("deviceManage");
}else{
System.out.println("擷取應用程式上下文失敗!");
return;
}
System.out.println("初始化系統服務!");
serverInitialized();
System.out.println("TcpServer初始化完成!");
}
/**
* 系統服務初始化
* device_type:由于隻需要知道采集儀,繼電器端口号
* 是以device_type設定為0
*/
public void serverInitialized(){
if(deviceManager == null){
System.out.println("擷取裝置管理器失敗!");
return;
}
List<Device> devices = this.deviceManager.load(0);
for(Device device : devices){
System.out.println("裝置ID-->"+device.getId());
System.out.println("采集儀端口号-->"+device.getProperty1());
System.out.println("繼電器端口号-->"+device.getProperty2());
System.out.println("所屬大棚-->"+device.getShed().getName());
}
//開啟序列槽伺服器-資料采集儀監聽線程
TcpServer daqServer = new TcpServer(5678);
TcpServer realyServer = new TcpServer(5679);
tcpManager = new Thread(new TcpManager());
if(daqServer.getServer() != null)
{
new Thread(daqServer).start();
}
if(realyServer.getServer()!=null){
new Thread(realyServer).start();
}
tcpManager.start();
}
/**
* 系統服務登出
*/
@SuppressWarnings("deprecation")
public void serverDestroyed(){
if(daqServer!=null){
try {
daqServer.getServer().close();
} catch (IOException e) {
System.out.println("資料采集儀服務線程關閉出錯 --> "+e.getMessage());
}
}
if(realyServer!=null){
try {
realyServer.getServer().close();
} catch (IOException e) {
System.out.println("繼電器服務線程關閉出錯 --> "+e.getMessage());
}
}
if(tcpManager!=null){
tcpManager.stop();
}
}
}
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完成初始化,如下:
<!--Spring ApplicationContext載入-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring ApplicationContext配置檔案的路徑,此參數用于後面的Spring-Contextloader -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<!-- 系統服務初始化 -->
<listener>
<listener-class>pams.servlet.SysInitServlet</listener-class>
</listener>
如果想在外部類中調用通過這種方法得到的spring對象,可以把springContext設為static,然後提供相應的get方法,此處由于不需要故設為private...