Spring @Configuration(非XML配置)用于注释驱动的任务
问题内容:
谁能解释使用@Scheduled批注而不进行任何XML配置的情况下如何实现任务的基本配置?我可以找到的所有示例至少使用最小的XML配置。例如:
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/>
所以我只是使用@Configuration批注和一堆@Bean批注。它们都在启动时实例化,但是带有@Scheduled的实例未运行。过去,在使用XML配置时,我已经成功使用了该批注,但是从来没有仅使用批注。
问题答案:
- 该`<task:annotation-driven
- />`注释结束了声明ScheduledAnnotationBeanPostProcessor在你的代码读取@Scheduled注释。参见此处:[http]( http://static.springsource.org/spring/docs/3.1.x/javadoc-
- api/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessor.html)
-
//static.springsource.org/spring/docs/3.1.x/javadoc-
api/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessor.html
。
这样就可以了<task:annotation-driven />
。要进行组件扫描,您需要使用AnnotationConfigApplicationContext。虽然不确定/如何与Web容器一起工作。