weixin_42505690 2013-07-01 10:58
浏览 2400
已采纳

Spring 3.2.3 使用 @Scheduled 注解的定时任务没有生效

用 Maven 构建的 Spring 3.2.3,尝试使用 @Scheduled 做定时任务,没有生效,但也未报任何错误。

环境:

pom.xml

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4.0.0
com.test
xyz
war
0.0.1-SNAPSHOT
xyz
http://maven.apache.org


org.springframework
spring-core
3.2.3.RELEASE


org.springframework
spring-beans
3.2.3.RELEASE


org.springframework
spring-context
3.2.3.RELEASE


org.springframework
spring-aop
3.2.3.RELEASE


org.springframework
spring-aspects
3.2.3.RELEASE


org.springframework
spring-web
3.2.3.RELEASE


log4j
log4j
1.2.17


applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd"
default-autowire="byName" default-lazy-init="true">

<context:component-scan base-package="com.test" />
<task:annotation-driven />

测试任务

package com.test.task;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class MyTask {

@Scheduled(cron = "0/5 * * * * ?")
public void doSth() {
    System.out.println(new Date());
}

}

但是,如果不使用 @Scheduled,用 标签则任务正常运行:


task:scheduled-tasks

/task:scheduled-tasks

求解?

展开全部

  • 写回答

2条回答 默认 最新

  • jinnianshilongnian 2013-07-01 12:44
    关注

    怀疑 default-lazy-init="true" 惹得祸

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部