Spring xml 配置在没有@Scheduled 的情况下启用@Async [英] Spring xml config enable @Async without @Scheduled
问题描述
使用 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
负责 @Async
和 org.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屋!