一直对于web.xml文件没啥研究,不是很清楚里边的加载顺序,直到最近要做个web端实时提醒问题来了,时时提醒是在Listener中,这里边引入service报错,提示service为null,查找到了好多方法都一直没法解决,中间走了好多弯路,知道最后在其他的同事的帮忙下解决了,还是上代码吧。
package com.eagle.listener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import org.comet4j.core.CometContext;
import org.comet4j.core.CometEngine;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.eagle.pojo.TblBusinesswaste;
import com.eagle.service.BusService;
import com.eagle.service.LoginService;
import com.eagle.service.QueryInfoService;
//实时通知
public class NoticeListener extends ContextLoaderListener implements ServletContextListener, HttpSessionListener,HttpSessionAttributeListener {
private static final String CHANNEL = "hello";
ServletContext application = null ;
BusService busService;
List<TblBusinesswaste> list = new ArrayList<TblBusinesswaste>();
public void contextDestroyed(ServletContextEvent sce) {
}
public void contextInitialized(ServletContextEvent sce) {
//super.contextInitialized(sce) ;
this.application = sce.getServletContext() ;
ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(application) ;
busService = (BusService) ctx.getBean("busService");
CometContext cc = CometContext.getInstance();
cc.registChannel(CHANNEL);//注册应用的channel
Thread myThread = new Thread(new MyThread(), "Sender App Module");
myThread.setDaemon(true);
myThread.start();
}
class MyThread implements Runnable {
List<TblBusinesswaste> lists = new ArrayList<TblBusinesswaste>();
public void run() {
while (true) {
try {
Thread.sleep(10000);
} catch (Exception ex) {
ex.printStackTrace();
}
CometEngine engine = CometContext.getInstance().getEngine();
engine.sendToAll(CHANNEL, Runtime.getRuntime().freeMemory()/1024);
}
}
}
@Override
public void sessionCreated(HttpSessionEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void sessionDestroyed(HttpSessionEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void attributeAdded(HttpSessionBindingEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void attributeRemoved(HttpSessionBindingEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void attributeReplaced(HttpSessionBindingEvent arg0) {
// TODO Auto-generated method stub
}
public ServletContext getApplication() {
return application;
}
public void setApplication(ServletContext application) {
this.application = application;
}
}
application.xml中就配置service的bean即可;
web.xml中配置
<listener>
<listener-class>com.eagle.listener.NoticeListener</listener-class>
</listener>
在此之前需要配置
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
即可。