SpringBoot:自定义Event的使用

1.创建自定义Event,使其继承ApplicationEvent

package cn.edu.tju.event;

import org.springframework.context.ApplicationEvent;

    public class MyEvent extends ApplicationEvent {
        private String msg;

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }

        public MyEvent(Object source, String msg){
        super(source);
        this.msg=msg;
    }
}

2.定义Event Listener来监听上述event的发生:

package cn.edu.tju.listener;

import cn.edu.tju.event.MyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component
public class MyEventListener {
    @EventListener
    public void gotMyEvent(MyEvent myEvent){
        System.out.println("event message is:"+myEvent.getMsg());
    }
}

3.触发自定义Event,

package cn.edu.tju.controller;

import cn.edu.tju.event.MyEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class EventController {
    @Autowired
    private ApplicationContext applicationContext;
    @RequestMapping("/createEvent")
    public String createEvent(String str){
        MyEvent myEvent=new MyEvent(this,"test event");
        applicationContext.publishEvent(myEvent);

        return "MyEvent triggered......";
    }
}

4.调用/createEvent接口,运行效果如下:
在这里插入图片描述
5.同时,也可以在启动spring boot程序时,手动注册Event Listener.
首先定义类来实现泛型接口ApplicationListener

package cn.edu.tju.listener;

import cn.edu.tju.event.MyEvent;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;

public class YourEventListener implements ApplicationListener<MyEvent> {

    @Override
    public void onApplicationEvent(MyEvent event) {
        System.out.println("test: "+event.getMsg());
    }
}

然后在启动程序时添加上述Listener:

package cn.edu.tju;

import cn.edu.tju.listener.MyEventListener;
import cn.edu.tju.listener.YourEventListener;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Start {
    public static void main(String[] args) {
        //SpringApplication.run(Start.class,args);
        SpringApplication springApplication=new SpringApplication(Start.class);
        //springApplication.setLazyInitialization(false);
        //springApplication.setBannerMode(Banner.Mode.OFF);
        springApplication.addListeners(new YourEventListener());
        springApplication.run(args);
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值