Python 官方文档:入门教程 => 点击学习
目录一、叙述spring的事件需要遵循如下流程Spring框架中事件二、上Demo示例1. pom文件2. 自定义一个事件继承ApplicationEvent3. 自定义一个监听器实
当Spring的事件(Application Event)为Bean和Bean之间的消息同步提供了支持。
当一个Bean处理完成一个任务之后,希望另外一个Bean知道并能做相应的处理,这时我们就需要让另外一个Bean监听当前Bean所发生的事件
Spring提供了以下5种标准的事件:
请求处理事件(RequestHandledEvent):在WEB应用中,当一个Http请求(request)结束触发该事件。
如果一个bean实现了ApplicationListener接口,当一个ApplicationEvent被发布以后,bean会自动被通知
<!-- https://mvnrepository.com/artifact/org.springframework/org.springframework.context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.context</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
import com.hrm.dto.ExamAnswer;
import org.springframework.context.ApplicationEvent;
public class AnswerEvent extends ApplicationEvent {
private static final long serialVersionUID = 1L;
public AnswerEvent(ExamAnswer answer) {
super(answer);
}
}
import com.hrm.dto.ExamAnswer;
import com.hrm.service.event.AnswerEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;
@Component
@EnableAsync
public class AnswerEventListener implements ApplicationListener<ExamAnswer> {
@Override
public void onApplicationEvent(AnswerEvent answerEvent) {
ExamAnswer answer = (ExamAnswer) answerEvent.getSource();
if(answer != null) {
super.save(answer);
}
}
}
import com.hrm.dto.ExamAnswer;
import com.hrm.service.event.AnswerEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Repository;
@Repository
public class ExamAnswerDaoImpl implements ExamAnswerDao {
@Autowired
private ApplicationContext applicationContext;
private void putAnswerToSpringEvent(ExamAnswer examAnswer) {
发送Spring Event 事件
applicationContext.publishEvent(new AnswerEvent(examAnswer));
}
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: 如何实现Spring Event(异步事件)
本文链接: https://www.lsjlt.com/news/195991.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0