springboot对监听器Listener的使用

springboot对监听器Listener的使用

监听器:listener是servlet规范中定义的一种特殊类。用于监听servletContext、HttpSession和servletRequest等域对象的创建和销毁事件。监听域对象的属性发生修改的事件。

    用于在事件发生前、发生后做一些必要的处理。其主要可用于以下方面:

        1、统计在线人数和在线用户

        2、系统启动时加载初始化信息

        3、统计网站访问量

        4、记录用户访问路径

方法1: 使用Springboot提供了RegistrationBean的子类ServletListenerRegistrationBean 用于注册Filter,完成过滤器的设置首先我们创建一个MyHttpSessionListener类

  

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

package com.example.spingbootdemo1.listener;

 

import javax.servlet.http.HttpSessionEvent;

import javax.servlet.http.HttpSessionListener;

 

public class MyHttpSessionListener implements HttpSessionListener {

    private static int onlineNum = 0;

 

    public void sessionCreated(HttpSessionEvent se) {

        onlineNum++;

    }

 

    public void sessionDestroyed(HttpSessionEvent se) {

        onlineNum--;

    }

}

 

然后我们创建一个控制器UserController

import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

public class UserController {
    @RequestMapping("/onlineCount")
    public String onlineCount(){
        return"网站当前在线人数为:"+ MyHttpSessionListener.onlineNum;
    }

    @RequestMapping("/index")
    public String index(HttpSession httpSession){
        //在访问首页的时候触发session的操作
        httpSession.setAttribute("username","xiadewang");return"首页";
    }
}

上面我们已经把监听器和控制层的工作完成了,但是我们知道springboot里面的监听器实际上底下就是spring和springmvc相关的东西,再下面就是servlet,在以前的老项目中我们需要在web.xml中多监听器做配置。而在springboot中我们没有了web.xml等相关xml配置文件,取而代之的是使用@Configuration注解在java代码中实现相关配置。于是我们可以创建一个配置类,代码如下:

@Configuration
public class MyMvcConfig {
    @Bean
    public ServletListenerRegistrationBean listenerRegist() {
        ServletListenerRegistrationBean srb=new ServletListenerRegistrationBean();
        srb.setListener(new MyHttpSessionListener());
        return srb;
    }
}

方法2: 使用@WebListener注解完成过滤器的设置

在MyHttpSessionListener上面加上@WebListener注解,

package com.example.spingbootdemo1.listener;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

@WebListener
public class MyHttpSessionListener implements HttpSessionListener {
    public static int onlineNum = 0;

    public void sessionCreated(HttpSessionEvent se) {
        onlineNum++;
    }

    public void sessionDestroyed(HttpSessionEvent se) {
        onlineNum--;
    }
}

然后在启动类上加@ServletComponentScan注解提供支持扫描webListener

/***
 * 使用@ServletComponentScan配合@WebListener后,务必要删除之前创建的MyMvcConfig这个类。实现过滤器配置二者选其一即可。
 * springboot默认会检索启动类所在包和子包下的所有spring容器相关的注解(比如@Controller、@Component等),但是像@WebFilter和@WebListener之类的不会
 */
@ServletComponentScan
@SpringBootApplication
public class Spingbootdemo1Application {
 public static void main(String[] args) {
        SpringApplication.run(Spingbootdemo1Application.class, args);
    }
}

参考https://www.cnblogs.com/nyhhd/p/12684177.html

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值