开发者社区> 问答> 正文

Spring@Scheduled 定时任没执行!

项目主要配置及代码如下,但是启动服务后,一直没执行这个方法,不知什么原因。请指点

package com.yl.reqPos;
import java.text.SimpleDateFormat;

import java.util.Date;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;

import org.springframework.stereotype.Component;
@Component
@Lazy(false)
public class Singer {
@Scheduled(cron  ="0/5 * * * * *")     //第二种方式  
    public void singing(){  
    Date date=new Date();  
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");  
    System.out.println(sdf.format(date));  
}  
}

applicationContext-jobs.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:task="http://www.springframework.org/schema/task" 
xmlns:context="http://www.springframework.org/schema/context" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd 
http://www.springframework.org/schema/task 
http://www.springframework.org/schema/task/spring-task-3.2.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.2.xsd"
default-lazy-init="false">
<context:component-scan base-package="com.yl.reqPos.*" />
queue-capacity="500" rejection-policy="CALLER_RUNS" /> 

展开
收起
小旋风柴进 2016-03-05 09:03:44 5459 0
1 条回答
写回答
取消 提交回答
  • 首先要配置spring.xml
    xmlns 多加下面的内容、

    [html] view plaincopy
    xmlns:task="http://www.springframework.org/schema/task"  

    然后xsi:schemaLocation多加下面的内容、

    [html] view plaincopy
    http://www.springframework.org/schema/task  
    http://www.springframework.org/schema/task/spring-task-3.1.xsd  

    最后是我们的task任务扫描注解

    [html] view plaincopy
    <task:annotation-driven/>  

    我的配置扫描位置是:

    [html] view plaincopy
    <context:annotation-config/>  
        <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>  
        <context:component-scan base-package="com.test"/>  

    扫描的是com.test这样的包下的内容、
    下面需要接口和实现(我的这几个Java文件都是com.test的包下的、

    [java] view plaincopy
    public interface IMyTestService {  
           public void myTest();  
    }  
    [java] view plaincopy
    @Component  //import org.springframework.stereotype.Component;  
    public class MyTestServiceImpl  implements IMyTestService {  
          @Scheduled(cron="0/5 * *  * * ? ")   //每5秒执行一次  
          @Override  
          public void myTest(){  
                System.out.println("进入测试");  
          }  
    }  

    执行后控制台就会打印出 进入测试 了
    需要注意的几点:
    1、spring的@Scheduled注解 需要写在实现上、
    2、 定时器的任务方法不能有返回值(如果有返回值,spring初始化的时候会告诉你有个错误、需要设定一个proxytargetclass的某个值为true、)
    3、实现类上要有组件的注解@Component

    2019-07-17 18:52:53
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第20讲):经典面试题与阿里等名企内部招聘求职面试技巧 立即下载
微服务架构模式与原理Spring Cloud开发实战 立即下载
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载

相关实验场景

更多