为什么Spring不运行我的@Scheduled方法?

ste*_*vls 26 spring annotations scheduler

我有点糊涂,因为我正在尝试使用@Scheduled注释,但Spring似乎没有找到我的方法.最终结果是,我的所有注释方法@Scheduled都没有被执行.

我用以下声明调用了Spring的任务魔法:

<beans> <!-- XMLNS, XSD declarations omitted for brevity -->

  <context:component-scan base-package="com.mypackage"/>

  <task:executor id="executor" pool-size="5"/>
  <task:scheduler id="scheduler" pool-size="5"/>
  <task:annotation-driven scheduler="scheduler" executor="executor"/>

</beans>
Run Code Online (Sandbox Code Playgroud)

我有一个看起来像这样的界面:

package com.mypackage;

public interface MyInterface {

    public void executePeriodically();
}
Run Code Online (Sandbox Code Playgroud)

有这样的相应impl:

package com.mypackage.impl;
// imports omitted for brevity

@Service
public class MyInterfaceImpl implements MyInterface {

    @Scheduled(cron = "0/5 * * * * ?")
    public void executePeriodically() {
        System.out.println("The time is now : " + new Date());
    }
}
Run Code Online (Sandbox Code Playgroud)

现在预期的结果是,我有一个非常吵闹的小家伙告诉我每5秒钟的时间......但实际上我什么都没有.我已尝试使用接口方法和impl方法上的注释,但这似乎没有改变任何东西.

我确定正在初始化执行程序和调度程序,因为我在日志中有以下内容:

INFO  - ThreadPoolTaskExecutor     - Initializing ExecutorService 
INFO  - XmlWebApplicationContext   - Bean 'executor' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
INFO  - XmlWebApplicationContext   - Bean 'executor' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
INFO  - ThreadPoolTaskScheduler    - Initializing ExecutorService  'scheduler'
INFO  - XmlWebApplicationContext   - Bean 'scheduler' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
Run Code Online (Sandbox Code Playgroud)

我不确定关于没有资格的那条线是否相关或是否是红鲱鱼.

目前,我通过宣布我的预定任务来解决这个问题:

<task:scheduled-tasks>
  <task:scheduled ref="sourceDocumentManagerImpl" method="deleteOldDocuments" cron="0 0 * * * ?"/>
</task:scheduled-tasks>
Run Code Online (Sandbox Code Playgroud)

虽然这很好用,但我更倾向于使用注释,因为直接在代码中看到对该方法的期望是多么方便.谁知道我可能做错了什么?为了记录,我使用的是Spring 3.0.4

谢谢你!

ahl*_*hll 18

添加"任务:注释驱动"?

<beans> <!-- XMLNS, XSD declarations omitted for brevity -->

  <context:component-scan base-package="com.mypackage"/>
  <task:annotation-driven/>
  <task:executor id="executor" pool-size="5"/>
  <task:scheduler id="scheduler" pool-size="5"/>
  <task:annotation-driven scheduler="scheduler" executor="executor"/>

</beans>
Run Code Online (Sandbox Code Playgroud)

参考http://howtodoinjava.com/2013/04/23/4-ways-to-schedule-tasks-in-spring-3-scheduled-example/

要么

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

只需在WebMvcConfig类上添加@EnableScheduling即可

@Configuration
@EnableWebMvc
@EnableAsync
@EnableScheduling
public class WebMvcConfig extends WebMvcConfigurerAdapter {
   /** Annotations config Stuff ... **/
}
Run Code Online (Sandbox Code Playgroud)

参考Spring Scheduler不起作用

  • 感谢@EnableScheduling! (2认同)

ste*_*vls 13

所以......看起来Spring 3.0.x中存在一个问题(至少3.0.4和3.0.5)与@Scheduled在AOP代理上发现注释有关.我有一个切入点声明@Scheduled用事务建议包装方法,这似乎是问题的根源.如果我删除了建议,则该作业将运行.如果我重新添加它,Spring无法为我的带注释方法找到并创建任务.

所以,我要向Spring人员提交一个bug,与此同时我不能手动声明我的任务.


小智 5

我通过将 default-lazy-init="false" 添加到我的 applicationContext.xml 来修复它。

<beans .....
    **default-lazy-init="false"**>
Run Code Online (Sandbox Code Playgroud)