用于注解驱动任务的 Spring @Configuration(非 xml 配置) [英] Spring @Configuration (non-xml configuration) for annotation-driven tasks

查看:35
本文介绍了用于注解驱动任务的 Spring @Configuration(非 xml 配置)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能解释如何在没有任何 XML 配置的情况下使用 @Scheduled 注释实现任务的基本配置?我能找到的所有示例都至少使用了最小的 XML 配置.例如:

Can anyone explain how to do achieve a basic configuration of a task using the @Scheduled annotation without any XML configuration? All the examples I can find use at least a minimal XML configuration. For example:

http://blog.springsource.com/2010/01/05/task-scheduling-simplifications-in-spring-3-0/

这使用了一个典型的:

  <context:component-scan base-package="org/springframework/samples/task/basic/annotation"/> 
  <task:annotation-driven/>

所以我只是使用带有一堆@Bean 注释的@Configuration 注释.它们都在启动时实例化,但带有 @Scheduled 的那个没有运行.过去我在使用 XML 配置时成功使用了该批注,但从来没有只使用批注.

So I'm just using a @Configuration annotation with a bunch of @Bean annotations. They are all instantiated at startup but the one with the @Scheduled does not run. I've used that annotation successfully in the past when using XML configuration, but never with annotations only.

推荐答案

<task:annotation-driven/> 注释最终声明了 ScheduledAnnotationBeanPostProcessor 来读取代码中的 @Scheduled 注释.请参阅此处:http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessor.html.

The <task:annotation-driven /> annotation ends up declaring a ScheduledAnnotationBeanPostProcessor to read the @Scheduled annotations in your code. See here: http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessor.html.

这会处理 行.要获得组件扫描,您需要使用 AnnotationConfigApplicationContext.不确定它是否/如何与网络容器一起使用.

That takes care of the <task:annotation-driven /> line. To get the component scanning you'll need to use AnnotationConfigApplicationContext. Not sure if/how that works with a web container though.

这篇关于用于注解驱动任务的 Spring @Configuration(非 xml 配置)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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