春天@Configuration(非XML配置)的注解驱动的任务 [英] Spring @Configuration (non-xml configuration) for annotation-driven tasks
问题描述
任何人都可以解释如何做到实现使用@Scheduled注解没有任何XML配置一个任务的基本配置?所有的例子,我可以找到使用至少一个最小的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:
<一个href=\"http://blog.springsource.com/2010/01/05/task-scheduling-simplifications-in-spring-3-0/\">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.
推荐答案
的&LT;任务:注解驱动/&GT;
标注结束声明ScheduledAnnotationBeanPostProcessor阅读@Scheduled注释在code。看这里:<一href=\"http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessor.html\" rel=\"nofollow\">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.
这需要照顾的&LT的;任务:注解驱动/&GT;
行。要获得组件扫描,您需要使用AnnotationConfigApplicationContext。不知道是否/如何与Web容器的作品虽然。
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.
这篇关于春天@Configuration(非XML配置)的注解驱动的任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!