Spring xml 配置在没有@Scheduled 的情况下启用@Async [英] Spring xml config enable @Async without @Scheduled

查看:40
本文介绍了Spring xml 配置在没有@Scheduled 的情况下启用@Async的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Spring 的 xml 配置,如何启用对 @Async 批注的扫描而不同时启用对 @Scheduled 批注的扫描?

Using Spring's xml configuration, how can I enable scanning of @Async annotations without also enabling scanning of @Scheduled annotations?

通常,您会使用 <task:annotation-driven/> 同时启用两者,但我试图仅在特定 Spring 配置文件处于活动状态时启用调度.

Normally, you would enable both simultaneously using <task:annotation-driven /> but I'm trying to enable scheduling only when a particular Spring profile is active.

使用 Spring JavaConfig,您可以分别使用 @EnableAsync@EnableScheduling.我正在维护一个已经存在几年的项目,并且仅使用基于 xml 和注释的配置,我不想将 JavaConfig 添加到组合中,除非这是唯一的方法.

Using Spring JavaConfig, you can use @EnableAsync and @EnableScheduling separately. I'm maintaining a project that's been around for a few years and uses only xml and annotation based config and I don't want to add JavaConfig to the mix unless it's the only way to do this.

推荐答案

手动添加相应的 bean,而不是使用命名空间.org.springframework.scheduling.annotation.AsyncAnnotationBeanPostProcessor 负责 @Asyncorg.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor 负责<代码>@Scheduled.

Instead of using the namespace add the respective beans manually. The org.springframework.scheduling.annotation.AsyncAnnotationBeanPostProcessor takes care of @Async and org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor takes care of @Scheduled.

通过手动注册 bean,您可以简单地将 ScheduledAnnotationBeanPostProcessor 移动到您希望它处于活动状态的配置文件中.

By registering the beans manually you can simply move the ScheduledAnnotationBeanPostProcessor to the profile where you want it active.

这篇关于Spring xml 配置在没有@Scheduled 的情况下启用@Async的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆