@Scheduled任务无法启动

spe*_*0ne 0 spring annotations spring-mvc quartz-scheduler

我正在尝试使用@Scheduled功能.我已经按照这个教程,但我不能让我的计划任务被执行.

我创造了一个工人:

@Component("syncWorker")
public class SyncedEliWorker implements Worker {
    protected Logger logger = Logger.getLogger(this.getClass());

    public void work() {
        String threadName = Thread.currentThread().getName();
        logger.debug("   " + threadName + " has began to do scheduled scrap with id=marketwatch2");
    }
}
Run Code Online (Sandbox Code Playgroud)

和SchedulingService:

@Service
public class SchedulingService {
    protected Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    @Qualifier("syncWorker")
    private Worker worker;

    @Scheduled(fixedDelay = 5000)
    public void doSchedule() {
        logger.debug("Start schedule");

        worker.work();
        logger.debug("End schedule");
    }
}
Run Code Online (Sandbox Code Playgroud)

并尝试在我的applicationcontext中进行不同的布线.最终版本如下:

<beans xmlns=...
       xmlns:task="http://www.springframework.org/schema/task"
       ...
       xsi:schemaLocation=" ..
                            http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">

    <context:annotation-config/>

    <task:annotation-driven executor="taskExecutor" scheduler="taskScheduler"/>
    <task:scheduler id="taskScheduler" pool-size="3"/>
    <task:executor id="taskExecutor" pool-size="3"/>

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

服务器启动时没有任何错误.

我错过了什么吗?

Boz*_*zho 8

<context:annotation-config />不检测bean - 它只处理声明的bean上的注释.这意味着你@Service实际上并没有变成一个bean.

<context:component-scan base-package="com.yourcomany" />改用.