1. Spring ApplicationListener 基础版

ApplicationListener系列文章

  1. ApplicationListener 基础版

  2. ApplicationListener 进阶版

  3. ApplicationListener源码版

  4. ApplicationListener 问题版

  5. ApplicationListener 扩展版

  6. ApplicationListener 总结版

简单使用范例

数据对象

@Data
@AllArgsConstructor
@NoArgsConstructor
public class DemoInfo implements Serializable {
    private Integer id;
    private String name;

    @Override
    public String toString() {
        return "DemoInfo{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

事件

public class DemoEvent extends ApplicationEvent {

    public DemoEvent(DemoInfo source) {
        super(source);
    }

    public DemoInfo getSource() {
        return (DemoInfo) super.getSource();
    }
}

监听器

public class DemoEventListener implements ApplicationListener<DemoEvent> {
    @Override
    public void onApplicationEvent(DemoEvent event) {
        DemoInfo source = event.getSource();
        System.out.println("demo event listener source:" + source.toString());
    }
}

发布事件

public static void main(String[] args) throws InterruptedException {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        //扫描监听器
        applicationContext.register(DemoEventListener.class);
        //启动容器
        applicationContext.refresh();
        //发布事件
        applicationContext.publishEvent(new DemoEvent(new DemoInfo(1, "demo")));
    }

输出结果

demo event listener source:DemoInfo{id=1, name=‘demo’}

进阶版

上述已经可以正常使用ApplicationListener,但是谁还没有点追求,进阶版:ApplicationListener进阶版

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值