有人能解释一下如何使用@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配置时成功地使用过该注释,但从来没有只使用过注释。
发布于 2011-08-02 06:04:56
<task:annotation-driven />注释最终声明了一个ScheduledAnnotationBeanPostProcessor来读取代码中的@Scheduled注释。请看这里:http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessor.html。
它负责处理<task:annotation-driven />行。要获得组件扫描,您需要使用AnnotationConfigApplicationContext。不过,我不确定这是否适用于web容器,或者如何使用web容器。
发布于 2012-08-23 05:01:09
只需在WebMvcConfig类上添加@EnableScheduling
@Configuration
@EnableWebMvc
@EnableAsync
@EnableScheduling
public class WebMvcConfig extends WebMvcConfigurerAdapter {
/** Annotations config Stuff ... **/
}发布于 2011-08-02 06:06:02
在Spring3.0中,您仍然需要少量的XML。然而,Spring3.1(仍处于测试阶段)引入了额外的注释选项来缩小差距,消除了对XML配置的任何需求。
查看this blog entry了解它是如何完成的。在生产代码中使用Spring的beta版本之前要非常小心--它们真的很不稳定。
https://stackoverflow.com/questions/6904956
复制相似问题