Spring-Boot 监听器

监听器Listener

概述

web监听器是servlet中特殊的类,能够帮助开发着监听web中的一些特定事件。

监听器的什么?

  • ServletContext – application
  • HttpSession - session
  • ServletRequest - request

监听器监听就是:拥有作用域的对象。而这个作用域的对象分别都有相同的方法

  • setAttribute
  • getAttribute
  • removeAttribute

不论三每个那种作用域只要调用setAttribute或者getAttribute或者删除removeAttribute就会进入到监听器的对应的方法中进行处理相应的逻辑。

应用场景

  • 初始化上下文,比如:spring容器的初始化,文件的解析
  • 会话的监听,比如:在线人数
    • demo版本
  • 监听客户的请求信息,比如:对某一个用户进行数据干预。
    • 直播,对平台中某些用户资源倾斜。

Springboot如何定义和注册监听器

监听ServletContext对象

package com.kuangstudy.config.listener;

import com.kuangstudy.pojo.User;
import com.kuangstudy.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

@Component
@Slf4j
public class ServletContextListener2 implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
       log.info("ServletContextListener2 --- > 监听器进来了.....");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        log.info("删除方法 ---- 监听器进来了.....");
    }
}

用来做:springioc容器的初始化使用的。

web.xml

<context>
    <context-name>contextConfigLocation</context-name
    <context-value>classpath:bean.xml</context-name
</context>

等价于

application.setAttribute("configurationLocation","classpath:bean.xml")

监听Http会话session对象

使用场景:监听session对象,以统计当前web网站在线用户的总数据量。如下:

1:创建一个session会话监听器,实现HttpSessionLisintener接口:

package com.kuangstudy.config.listener;

import com.kuangstudy.pojo.User;
import com.kuangstudy.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

@Component
public class LoginSessionListener implements HttpSessionListener {

    // 1: 定义一个在线人数的计数器
    public static Integer count = 0;

    @Override
    public void sessionCreated(HttpSessionEvent se) {
        System.out.println("新用户上线了...");
        count++;
        ServletContext application = se.getSession().getServletContext();
        application.setAttribute("personcount", count);
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        System.out.println("用户下线了...");
        count--;
        ServletContext application = se.getSession().getServletContext();
        application.setAttribute("personcount", count);
    }
}

2:定义登录,退出,查询在线人数

package com.kuangstudy.web;

import com.kuangstudy.pojo.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
public class LoginController {

    @GetMapping("/count/onelineuser")
    public String getOnlinePersoncount(HttpServletRequest request) {
        Integer personcount = (Integer) request.getServletContext().getAttribute("personcount");
        return  (personcount == null ? "0" : personcount + "");
    }

    @GetMapping("/logined")
    public String logined(HttpSession session) {
        // 相同的session信息,它的sessionid是一致,监听器只会进入一次。
        session.setAttribute("user", new User(1L, "yykk", "zhangsan"));
        return "success";
    }

    @GetMapping("/logout")
    public String logout(HttpSession session) {
        // session.invalidate --- 会进入到 sessionDestroyed
        session.invalidate();
        return "success";
    }
}

分别访问:

http://localhost:8987/count/onelineuser ----- 查看在线人数

http://localhost:8987/logined ------登录

http://localhost:8987/logout ---- 退出

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值