spring事件(ApplicationEvent、ApplicationListener)

事件定义

1、继承ApplicationEvent

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

@Getter
@Setter
public class MessageEvent extends ApplicationEvent {

    private String content;

    public MessageEvent(Object source, String content) {
        super(source);// 事件源
        this.content = content;
    }

}

2、普通的POJO

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

@Getter
@Setter
public class MessageEvent {

    private String content;

    public MessageEvent(String content) {
        this.content = content;
    }

}

事件监听

1、实现ApplicationListener接口

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * 发送微信handler
 */
@Component
@Slf4j
public class SendWeiXinEventHandler implements ApplicationListener<MessageEvent> {

    @Override
    @Async
    public void onApplicationEvent(MessageEvent event) {
        log.info("send wei xin, content:{}",event.getContent());
    }

}

2、使用@EventListener注解

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * 发送微信handler
 */
@Component
@Slf4j
public class SendWeiXinEventHandler {

    @EventListener
    @Async // 异步执行,不加此注解,默认是同步的,消息处理会阻塞主线程
    public void onApplicationEvent(MessageEvent event) {
        log.info("send wei xin, content:{}",event.getContent());
    }

}

事件发布

发布事件使用spring的ApplicationEventPublisher对象

1、发布继承ApplicationEvent的事件

	@Resource
    private ApplicationEventPublisher publisher;
    
    ...
    
	MessageEvent event = new MessageEvent(this, "message");
    publisher.publishEvent(event);

2、发布普通POJO事件

	@Resource
    private ApplicationEventPublisher publisher;
    
    ...
    
	MessageEvent event = new MessageEvent("message");
    publisher.publishEvent(event);

注意事项

使用@Async进行异步处理事件时,springboot项目启动需启加上@EnableAsync注解

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;

@SpringBootApplication
@EnableAsync
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

}

spring容器中是通过ApplicationEvent类和ApplicationListener接口来处理事件的如果某个bean实现了ApplicationListener接口并被部署到容器中,那么每次对应的ApplicationEvent被发布到容器中时,都会通知该bean,这是典型的观察者模式

spring的事件默认是同步的,即调用publisEvent()方法发布事件后,它会处于阻塞状态, 直到onApplicationEvent接收到事件并处理完返回之后才会继续往下执行, 这种单线程同步的好处是可以进行事务管理

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值