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 配置时成功地使用了该注释,但从未仅使用注释。
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/
This uses a typical:
<context:component-scan base-package="org/springframework/samples/task/basic/annotation"/>
<task:annotation-driven/>
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只需在 WebMvcConfig 类上添加 @EnableScheduling
Just add @EnableScheduling on you WebMvcConfig class
注解最终声明一个 ScheduledAnnotationBeanPostProcessor 来读取代码中的 @Scheduled 注解。请参阅此处:http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessor.html。这负责
行。要进行组件扫描,您需要使用 AnnotationConfigApplicationContext。但不确定它是否/如何与 Web 容器一起工作。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.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 3.0 中,您仍然需要一点 XML。然而,Spring 3.1(仍处于测试阶段)引入了额外的注释选项来缩小差距,消除了对 XML 配置的任何需要。
请参阅此博客文章了解具体方法完成了。不过,在生产代码中使用 Spring 的 beta 版本之前要非常小心 - 它们确实不稳定。
In Spring 3.0, you still need that little bit of XML. However, Spring 3.1 (still in beta) introduces additional annotation options to close the gap, removing any need for XML config.
See this blog entry for how it's done. Be very careful before using beta versions of Spring in production code, though - they really are unstable.
到目前为止的答案对于 Spring 的早期版本都有帮助。
下面是一个更适合 Spring 4 的类:
假设您的主 Application 类为组件扫描进行了注释,如下所示:
在该包内,您有一个如下所示的作业类:
请注意,您注释的方法@Scheduled 必须返回 void 并且您的 cron 表达式需要有 6 个字符(此处显示的示例每秒运行一次,这使得测试您的作业更容易)。
您还需要 @Configuration 和 @EnableScheduling 的类级别注释才能完成这项工作。两者本身似乎都被忽视了。
如需进一步阅读,请参阅 Spring 4 启用调度参考文档。
The answers so far are all helpful for earlier versions of Spring.
Here's one that's a bit more tailored to Spring 4:
Assume that you have your main Application class annotated for Component scan like this:
And inside of that package, you have a job class that looks like this:
Note that the method that you annotate with @Scheduled must return void and that your cron expression needs to have 6 characters (the example shown here runs every second, which makes testing what your job does easier).
You also need the class level annotations of both @Configuration and @EnableScheduling to make this work. Either by themselves seems to get ignored.
For further reading here is the Spring 4 Enable Scheduling Reference Doc.