spring定时任务

来源:互联网 发布:财务 知乎 编辑:程序博客网 时间:2024/05/17 23:21


我需要定时让服务器对用户进行扣费,于是想到了spring中的定时任务。(其实有多种方式可以实现【task,Quartz,JDK Timer support】)


http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/scheduling.html

这个是spring提供的官方文档。

了解了一下里面的内容之后按照注释@Scheduled的方式进行试验未成功过。


将我试验过的方法的代码贴在下面。

spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"
default-lazy-init="false">

  <!-- scheduler -->
<task:scheduled-tasks>
<task:scheduled ref="TaskJob" method="pay" cron = "*/2 * * * * MON-FRI"/>
</task:scheduled-tasks>
    <bean id="TaskJob" class="edu.nju.onlinestock.schedule.TaskJob">
</bean>

【由于上面的引用非常容易错所以就贴过来了】

【spring的官网说spring3之后才开始支持task但是程序可以运行...摊手】

定时任务类

import org.springframework.scheduling.annotation.Scheduled;  
import org.springframework.stereotype.Component;


public class TaskJob {
   
    public void pay() {
        System.out.println("hiahiahiahiahiahia!!!!");

    }
}



"0 15 10 15 * ?" 每月15号的10:15触发


0 0
原创粉丝点击