@Aync是通过AsyncAnnotationBeanPostProcessor来处理。其在setBeanFactory会设置AsyncAnnotationAdvisor,其代码如下

public void setBeanFactory(BeanFactory beanFactory) {super.setBeanFactory(beanFactory);AsyncAnnotationAdvisor advisor = new AsyncAnnotationAdvisor(this.executor, this.exceptionHandler);if (this.asyncAnnotationType != null) {advisor.setAsyncAnnotationType(this.asyncAnnotationType);}advisor.setBeanFactory(beanFactory);this.advisor = advisor;}

在其父类AbstractAdvisingBeanPostProcessor#postProcessAfterInitialization中会创建代理

public Object postProcessAfterInitialization(Object bean, String beanName) {if (this.advisor == null || bean instanceof AopInfrastructureBean) {// Ignore AOP infrastructure such as scoped proxies.return bean;}if (bean instanceof Advised) {Advised advised = (Advised) bean;if (!advised.isFrozen() && isEligible(AopUtils.getTargetClass(bean))) {// Add our local Advisor to the existing proxy's Advisor chain...if (this.beforeExistingAdvisors) {advised.addAdvisor(0, this.advisor);}else {advised.addAdvisor(this.advisor);}return bean;}}if (isEligible(bean, beanName)) {ProxyFactory proxyFactory = prepareProxyFactory(bean, beanName);if (!proxyFactory.isProxyTargetClass()) {evaluateProxyInterfaces(bean.getClass(), proxyFactory);}proxyFactory.addAdvisor(this.advisor);customizeProxyFactory(proxyFactory);return proxyFactory.getProxy(getProxyClassLoader());}// No proxy needed.return bean;}

@TransactionEventListener是通过EventListenerMethodProcessor来处理,在所有实例都实例化后,会调用afterSingletonsInstantiated方法,解析得到ApplicationListener添加到ApplicationContext中。

private void processBean(final String beanName, final Class<?> targetType) {if (!this.nonAnnotatedClasses.contains(targetType) &&AnnotationUtils.isCandidateClass(targetType, EventListener.class) &&!isSpringContainerClass(targetType)) {Map<Method, EventListener> annotatedMethods = null;try {annotatedMethods = MethodIntrospector.selectMethods(targetType,(MethodIntrospector.MetadataLookup<EventListener>) method ->AnnotatedElementUtils.findMergedAnnotation(method, EventListener.class));}catch (Throwable ex) {// An unresolvable type in a method signature, probably from a lazy bean - let's ignore it.if (logger.isDebugEnabled()) {logger.debug("Could not resolve methods for bean with name '" + beanName + "'", ex);}}if (CollectionUtils.isEmpty(annotatedMethods)) {this.nonAnnotatedClasses.add(targetType);if (logger.isTraceEnabled()) {logger.trace("No @EventListener annotations found on bean class: " + targetType.getName());}}else {// Non-empty set of methodsConfigurableApplicationContext context = this.applicationContext;Assert.state(context != null, "No ApplicationContext set");List<EventListenerFactory> factories = this.eventListenerFactories;Assert.state(factories != null, "EventListenerFactory List not initialized");for (Method method : annotatedMethods.keySet()) {for (EventListenerFactory factory : factories) {if (factory.supportsMethod(method)) {Method methodToUse = AopUtils.selectInvocableMethod(method, context.getType(beanName));ApplicationListener<?> applicationListener =factory.createApplicationListener(beanName, targetType, methodToUse);if (applicationListener instanceof ApplicationListenerMethodAdapter) {((ApplicationListenerMethodAdapter) applicationListener).init(context, this.evaluator);}context.addApplicationListener(applicationListener);break;}}}if (logger.isDebugEnabled()) {logger.debug(annotatedMethods.size() + " @EventListener methods processed on bean '" +beanName + "': " + annotatedMethods);}}}}

@TransactionalEventListener与@Async实现事务提交后异步处理事件相关推荐

  1. java 异步调用 事务_@Transactional事务提交后触发异步方法

    一.问题复现 1.场景 2个service方法, 方法A中调用方法B. 方法A 是核心业务方法,涉及多张表数据变更,为了保持数据一致,用spring事务注解:@Transactional(rollba ...

  2. spring中事务提交后再发MQ消息

    本篇博文记录一个小的知识点,在spring框架下的业务代码中同时有数据库操作,有MQ消息发送,怎么控制消息发送在事务提交之后,有问题可及时在本博客下留言,或者在个人博客留言 业务场景: 在一个加了事务 ...

  3. 如何控制MySQL事务提交后,刷redo-log的策略?

    既然涉及到事务提交,那么我们就是以InnoDB来说明的. MySQL有一个参数,能够控制事务提交时,刷redo log的策略.该参数为:innodb_flush_log_at_trx_commit. ...

  4. mysql 提交事务_MySQL事务提交过程

    一.MySQL事务提交过程(一) MySQL作为一种关系型数据库,已被广泛应用到互联网中的诸多项目中.今天我们来讨论下事务的提交过程. 由于mysql插件式存储架构,导致开启binlog后,事务提交实 ...

  5. 列表数据提交_MySQL事务提交过程(一)

    MySQL作为一种关系型数据库,已被广泛应用到互联网中的诸多项目中.今天我们来讨论下事务的提交过程. MySQL体系结构 由于mysql插件式存储架构,导致开启binlog后,事务提交实质是二阶段提交 ...

  6. java spring 事务提交_Spring 事务提交之后再执行操作

    Spring代码实现: import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.ster ...

  7. mysql事务回滚是什么意思_Mysql事务提交及事务回滚是什么意思

    本篇文章主要给大家介绍mysql事务提交及事务回滚的相关知识. 事务可以说是一段sql 语句的批处理,但是这个批处理是一个atom(原子) ,不可分割,要么都提交执行,要么回滚(rollback)都不 ...

  8. Java-JDBC【之】事务介绍、事务特性、操作事务(事务提交、异常回滚)

    Java-JDBC[之]事务介绍.事务特性.操作事务(事务提交.异常回滚) 1.数据库事务 1.1.介绍 1.2.事务特性(ACID) 1.3.隔离性(Isolation),带来的问题与处理 1.4. ...

  9. 如何在spring事务提交成功后再进行异步操作

    业务场景: 我们经常会有些方法会调用一些方法异步执行,比如入库操作后要异步发送mq消息,发送短信或者发布事件等等 但是往往我们的业务方法总是包含事务的,要么全部成功,要么全部失败. 有的时候事务执行失 ...

最新文章

  1. 虚拟服务器目录,服务器虚拟主机目录
  2. .Net软件测试化之道 [James D.MCCaffrey]
  3. VMware View 5.0 Ready! 资源汇总(持续更新)
  4. altera fpga 型号说明_A/X家FPGA架构及资源评估
  5. 蔡高厅老师 - 高等数学阅读笔记 - 06 - 高阶导数 隐函数- 02 (28、29、30、31)
  6. Windows下常用的100个CMD指令以及常见的操作
  7. labelImg 工具
  8. 晨哥真有料丨太快得到是不是就不珍惜了?这锅,男生真的不背!
  9. JDBC ResultSet分析
  10. 免费下载!阿里云重磅发布《云网络白皮书》,开启云网络3.0时代
  11. 写单例模式以及防止反射破坏
  12. 计算机b类b级2020,CPU天梯图2020_笔记本CPU天梯图2020最新版【7月更新】-太平洋IT百科...
  13. [计算机网络]网络层IP协议 选路算法
  14. 电脑出现0xv0000225无法开机的两种解决方法
  15. python静态函数
  16. 在 LaTeX 中定义变量
  17. 科技爱好者周刊(第 202 期):三个有启发的学习方法
  18. 分页插件PageHelper失效
  19. 应用卸载后依然存在的文件目录
  20. 关于树莓派4B安装桌面控件wbar和conky解决报错的一种方案

热门文章

  1. python抓取新浪微博评论并分析
  2. 20145217信息安全系统设计基础第11周学习总结
  3. Runtime实战之定制TabBarItem大小
  4. [恢]hdu 2098
  5. 用jquery的ajax功能获取网站alexa的方法
  6. python是动态编程语言吗-python是一种跨平台、开源、免费的高级动态编程语言,对么...
  7. python读数据-如何用Python读取开放数据?
  8. python详细安装教程环境配置-python环境配置详细步骤
  9. python3.6手册中文版-python3.6文档中文版
  10. 案例驱动python编程入门-python ddt数据驱动实例代码分享