springboot事件监听

新建项目

  • ide: idea
  • Create New Project -> Spring Initializr ->next-> 添加项目信息

springboot application自带监听事件

springboot自带监听事件有:

1. ApplicationStartingEvent:在Spring最开始启动的时候触发
2. ApplicationEnvironmentPreparedEvent:在Spring已经准备好上下文但是上下文尚未创建的时候触发
3. ApplicationPreparedEvent:在Bean定义加载之后、刷新上下文之前触发
4. ApplicationStartedEvent:在刷新上下文之后、调用application命令之前触发
5. ApplicationReadyEvent:在调用applicaiton命令之后触发
6. ApplicationFailedEvent:在启动Spring发生异常时触发

listener

新建listener文件夹,再这个文件夹下新建ApplicationStartedEventListener ,以及ApplicationReadyEventListener。

ApplicationStartedEventListener

package indi.xzw.event_listener.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class ApplicationStartedEventListener implements ApplicationListener<ApplicationStartedEvent> {
    @Override
    public void onApplicationEvent(ApplicationStartedEvent event) {
        log.info("......ApplicationStartedEvent......");
    }
}

ApplicationReadyEventListener

package indi.xzw.event_listener.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class ApplicationReadyEventListener implements ApplicationListener<ApplicationReadyEvent> {

    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        log.info("......ApplicationReadyEvent......");
    }

}

运行

2020-10-20 17:16:03.330  INFO 6240 --- [           main] i.x.e.EventListenerApplication           : Starting EventListenerApplication on DESKTOP-7GMIRBP with PID 6240 (D:\xzw\demo\springBoot\springBoot_eventListener\target\classes started by 14784 in D:\xzw\demo\springBoot\springBoot_eventListener)
2020-10-20 17:16:03.334  INFO 6240 --- [           main] i.x.e.EventListenerApplication           : No active profile set, falling back to default profiles: default
2020-10-20 17:16:04.050  INFO 6240 --- [           main] i.x.e.EventListenerApplication           : Started EventListenerApplication in 1.21 seconds (JVM running for 3.633)
2020-10-20 17:16:04.051  INFO 6240 --- [           main] i.x.e.l.ApplicationStartedEventListener  : ......ApplicationStartedEvent......
2020-10-20 17:16:04.053  INFO 6240 --- [           main] i.x.e.l.ApplicationReadyEventListener    : ......ApplicationReadyEvent......

Process finished with exit code 0

注解的方式添加事件

新建ApplicationStartedEventListener2

package indi.xzw.event_listener.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class ApplicationStartedEventListener2 {
    @EventListener
    public void onStarteddEvent(ApplicationStartedEvent applicationStartedEvent2)  {
        log.info("......ApplicationStartedEvent......2...");
    }
}

运行

2020-10-20 17:57:23.754  INFO 18184 --- [           main] i.x.e.l.ApplicationStartedEventListener2 : ......ApplicationStartedEvent......2...
2020-10-20 17:57:23.756  INFO 18184 --- [           main] i.x.e.l.ApplicationStartedEventListener  : ......ApplicationStartedEvent......
2020-10-20 17:57:23.758  INFO 18184 --- [           main] i.x.e.l.ApplicationReadyEventListener    : ......ApplicationReadyEvent......

自定义监听事件

自定义事件

新建event文件夹并再该文件夹下新建MyEvent

package indi.xzw.event_listener.event;

import lombok.Getter;
import lombok.Setter;
import org.springframework.context.ApplicationEvent;

import java.util.Map;

@Getter
@Setter
public class MyEvent extends ApplicationEvent {
    public MyEvent(Object source, Map<String, String> map) {
        super(source);
        this.map = map;
    }

    private Map<String, String> map;
}

自定义监听器

在listener文件夹下新建MyEventListener

package indi.xzw.event_listener.listener;

import indi.xzw.event_listener.event.MyEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class MyEventListener {
    @EventListener
    public void processMyEvent(MyEvent myEvent){
        log.info("----- processMyEvent {}",myEvent.getMap().toString());
        // 测试,模拟实际项目中的业务处理
        myEvent.getMap().put("name", "xzw");
        log.info(myEvent.getMap().toString());
    }
}

发布事件

新建controller文件夹并在该文件夹下新建MyEventController

package indi.xzw.event_listener.controller;


import indi.xzw.event_listener.event.MyEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
public class MyEventController {
    @Autowired
    private ApplicationContext publisher;

    @ResponseBody
    @RequestMapping("/publisher")
    public String publisher(){
        Map<String, String> map = new HashMap<>();
        map.put("name", "xiazhognwei");
        map.put("city", "nanjing");
        MyEvent event = new MyEvent(this,map);
        publisher.publishEvent(event);
        return "success";
    }
}

运行

访问http://127.0.0.1:8080/publisher

81

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×