Springboot下自定义监听器的使用

本文探讨了Servlet与Springboot的关系,指出Springboot的web容器基于Servlet实现。接着介绍了监听器的分类,如HttpSessionListener、ServletContextListener和ServletRequestListener,并讲解了在Springboot中如何自定义监听器来监控内存对象的变化,例如跟踪名为tom和sec的猪的信息更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Servlet和Springboot的关系

spring boot 三大特性:

  • 组件自动装配:webMVC、webFlux、JDBC等(@EnableAutoConfiguration,@Configuration)
  • 嵌入式Web容器:Tomcat、Jetty以及undertow(简单说下我的理解,Spring的核心功能是IOC和DI。那么web部分,在springboot中肯定是做嵌入式的集成。Springboot中的web容器分为两类:1,web
    Servlet容器:Tomcat, Jetty, undertow。2,web Reactive容器:Netty, Web Server)
  • 生产准备特性:指标、健康检查、外部化部署等
    所以springboot的web容器是基于servelet实现的。

监听器的分类和作用

  1. HttpSessionListener(统计用户数量)
  2. ServletContextListener(加载初始化信息)
  3. ServletRequestListener(监控request内置对象)
  4. 自定义监听事件? extends ApplicationEvent和监听器? implements AppilicationListener

Springboot下自定义监听器的使用

1, 创建监听事件
2, 创建监听器
3, 发布事件
代码说明:
我想要监控内存中的两个对象,第一个为名为tom的猪,第二个为名为sec的猪。这两只猪在spring容器启动后,会交给spring管理,每当有用户请求controller层修改这两只猪的信息,都会通过监听器监听到,并将信息打印出来。

//PigFactory:将两只猪注入Spring容器中。
@Configuration
public class PigFactory {

    @Bean(name = "tom")
    public Pig getPigTom(){
        return new Pig("tom", "i am tom");
    }

    @Bean(name = "sec")
    public Pig getPigSec(){
        return new Pig("Sec", "i am sec");
    }
}

//MyEvent:自定义监听事件,继承ApplicationEvent类。
@Data
public class MyEvent extends ApplicationEvent {

    private Pig pig;

    public MyEvent(Object o, Pig pig){
        super(o);
        this.pig = pig;
    }
}
//MyListener 自定义监听器,实现ApplicationListener接口,重写监听逻辑。这里只做简单打印。
@Component
public class MyListener implements ApplicationListener<MyEvent> {

    /**
     * 对监听到的事件进行处理。
     * @param event
     */
    @Override
    public void onApplicationEvent(MyEvent event){
        System.out.println("我监听到了:" + event.getPig().getMsg());
    }
}

@Component
public class ListenerBind {

    /**
     * 上下文对象
     */
    @Resource
    private ApplicationContext applicationContext;

    public void publish(Pig pig){
        // 通过上下文对象发布监听
        applicationContext.publishEvent(new MyEvent(this, pig));
    }
}

//PigController:controller层,用户通过调用该类方法,来修改猪的信息,并发布事件。这样监听器就可以监听到。注意:每次修改值以后都需要发布。
@RestController
@RequestMapping("/pig")
public class PigController {

    @Autowired
    private ListenerBind listenerBind;

    /**
     * 通过spring 注入第一只猪,然后web每请求一次进行一次修改,测试监听器是否进行监听。
     */
    @Resource(name = "tom")
    private Pig tom;

    /**
     * 通过按照类型注入和@Qualifier注解拿到pig-sec
     */
    @Autowired
    @Qualifier("sec")
    private Pig sec;


    /**
     * 每次请求时候都去更改pig-tom的msg。
     */
    @GetMapping(value = "/configTom")
    public void configTome(@RequestParam String msg @RequstParam(value = “repub”, required = false) boolean repub) {
        tom.setMsg(msg);
		if(repub){
			listenerBind.publish(tom);
		}else{
	// 以此测试事件每次发生变化时候,都需要重新发布。否则监听器监听不到。
		}
    }

    @GetMapping(value = "/configSec")
    public void configSec(@RequestParam String msg) {
        sec.setMsg(msg);
        listenerBind.publish(sec);
    }
}

效果如图:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值