公众号:架构师与哈苏
关注公众号进入it交流群! 公众号:架构师与哈苏 不定时都会推送一些实用的干货。。。
事件对象
package com.wzq.demoftl.observer;

import org.springframework.context.ApplicationEvent;

public class ObserverEvent extends ApplicationEvent {

    private String name;

    /**
     * Create a new {@code ApplicationEvent}.
     *
     * @param source the object on which the event initially occurred or with
     *               which the event is associated (never {@code null})
     */
    public ObserverEvent(Object source, String name) {
        super(source);
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

监听事件
package com.wzq.demoftl.observer;

import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

@Component
public class ObserverListener implements ApplicationListener<ObserverEvent> {
    @Override
    public void onApplicationEvent(ObserverEvent event) {
        System.out.println("event = " + event.getName());
    }
}

发布
@Autowired
private ApplicationContext applicationContext;

@RequestMapping("testL")
public void testListener(){
    this.applicationContext.publishEvent(new ObserverEvent(this, "吴志奇"));

}

posted on 2021-08-04 15:26  公众号/架构师与哈苏  阅读(20)  评论(0编辑  收藏  举报