zoukankan      html  css  js  c++  java
  • Springboot事件监听实例

    创建事件源

    package com.example.demo;
    
    import org.springframework.context.ApplicationEvent;
    import org.springframework.stereotype.Component;
    
    /**
     * @author lyd
     * @Description: 创建事件发布对象实体
     * @date 2021/6/1818:14
     */
    
    public class PushEvent extends ApplicationEvent {
    
    	private String msg;
    
    	public PushEvent(Object source, String msg) {
    		super(source);
    		this.msg = msg;
    	}
    
    	public String getMsg() {
    		return msg;
    	}
    
    	public void setMsg(String msg) {
    		this.msg = msg;
    	}
    }
    

    创建两个监听器

    package com.example.demo;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.context.event.EventListener;
    import org.springframework.stereotype.Service;
    
    /**
     * @author lyd
     * @Description: 监听者1
     * @date 2021/6/1818:16
     */
    @Slf4j
    @Service
    public class EventListenerOne {
    
    	@EventListener
    	public void enentListener(PushEvent pushEvent) {
    		log.info(this.getClass().getSimpleName() + "监听到数据:" + pushEvent.getMsg());
    	}
    
    
    }
    
    package com.example.demo;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.context.event.EventListener;
    import org.springframework.stereotype.Service;
    
    /**
     * @author lyd
     * @Description: 监听者2
     * @date 2021/6/1818:16
     */
    @Slf4j
    @Service
    public class EventListenerTwo {
    
    	@EventListener
    	public void enentListener(PushEvent pushEvent) {
    		log.info(this.getClass().getSimpleName() + "监听到数据:" + pushEvent.getMsg());
    	}
    
    
    }
    

    发布事件

    package com.example.demo;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.ApplicationEvent;
    import org.springframework.context.ApplicationEventPublisher;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    
    /**
     * @author lyd
     * @Description: 测试类
     * @date 2021/6/1818:31
     */
    @Slf4j
    @SpringBootApplication
    @RestController
    public class TestDemo {
    
    	@Autowired
    	private ApplicationEventPublisher  applicationEventPublisher;
    
    
    	@RequestMapping("publish")
    	public void publish() {
    
    		String msg = "哟哟";
    		log.info("发送哟哟:" + msg);
    
    		ApplicationEvent event = new PushEvent(this, msg);
    		applicationEventPublisher.publishEvent(event);
    
    	}
    
    
    }
    

    启动服务器,访问路径 http://localhost:8080/publish

  • 相关阅读:
    使用MingGW-w64 Build Script 3.6.7搭建ffmpeg编译环境
    ffmpeg精简编译
    CListCtrl死锁的问题
    VC程序禁用提示框
    rtmp协议分析
    [置顶] zabbix发送告警
    [置顶] 个人微信号发送zabbix告警信息
    [置顶] 一个简单好用的zabbix告警信息发送工具
    [置顶] zabbix告警信息-lykchat信息发送系统
    模拟登陆web微信的流程和参数细节
  • 原文地址:https://www.cnblogs.com/lyd447113735/p/14915046.html
Copyright © 2011-2022 走看看