Spring @Configuration(非xml配置)用于注解驱动的任务

发布于 2024-11-27 15:42:17 字数 569 浏览 0 评论 0原文

谁能解释如何使用 @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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

单身情人 2024-12-04 15:42:17

只需在 WebMvcConfig 类上添加 @EnableScheduling

@Configuration
@EnableWebMvc
@EnableAsync
@EnableScheduling
public class WebMvcConfig extends WebMvcConfigurerAdapter {
   /** Annotations config Stuff ... **/
}

Just add @EnableScheduling on you WebMvcConfig class

@Configuration
@EnableWebMvc
@EnableAsync
@EnableScheduling
public class WebMvcConfig extends WebMvcConfigurerAdapter {
   /** Annotations config Stuff ... **/
}
自在安然 2024-12-04 15:42:17

注解最终声明一个 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.

梦亿 2024-12-04 15:42:17

在 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.

疯到世界奔溃 2024-12-04 15:42:17

到目前为止的答案对于 Spring 的早期版本都有帮助。
下面是一个更适合 Spring 4 的类:

假设您的主 Application 类为组件扫描进行了注释,如下所示:

@ComponentScan({"com.my.class"})

在该包内,您有一个如下所示的作业类:

@Configuration
@EnableScheduling
public class MyJobClass {
@Scheduled (cron = "* * * * * *")
public void runJob() throws DocumentException {
    thingsToDoOnSchedule(); 
   }
}

请注意,您注释的方法@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:

@ComponentScan({"com.my.class"})

And inside of that package, you have a job class that looks like this:

@Configuration
@EnableScheduling
public class MyJobClass {
@Scheduled (cron = "* * * * * *")
public void runJob() throws DocumentException {
    thingsToDoOnSchedule(); 
   }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文