weixin_42505690
weixin_42505690
2013-07-01 18:58
浏览 2.0k
已采纳

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
    jinnianshilongnian 2013-07-01 20:44
    已采纳

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

    点赞 评论
  • qing930419
    qing930419 2015-12-15 10:20

    应该这样配置吧

    点赞 评论

相关推荐