1.启动类上添加注解
@ServletComponentScan
@SpringBootApplication() @ComponentScan(basePackages={"com"}) @ServletComponentScan //scans from the package of the annotated class (@WebServlet, @WebFilter, and @WebListener) public class DemoApplication { public static void main(String[] args) { //此设置一定要在SpringApplication.run(DemoApplication.class, args); 前面进行设置 //System.setProperty("spring.devtools.restart.enabled","false"); SpringApplication.run(DemoApplication.class, args); } }
2.监听类
@WebListener public class MyHttpSessionListener implements HttpSessionListener{ public static int online = 0; @Override public void sessionCreated(HttpSessionEvent arg0) { // TODO Auto-generated method stub System.out.println("创建session"); online ++; System.out.println("当前在线人数:"+online); } @Override public void sessionDestroyed(HttpSessionEvent arg0) { // TODO Auto-generated method stub System.out.println("销毁session"); online --; } }