spring-task解决定时问题

Posted F1yBear

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring-task解决定时问题相关的知识,希望对你有一定的参考价值。

 

 spring3以上版本,spring-content自带 spring-task ,来解决工程中的定时问题

 基于注解配置spring定时任务

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:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd ">

    <mvc:annotation-driven />

    <context:component-scan base-package="com" />
    
     <!--开启该组件就可以,基于注解使用定时任务啦 -->
    <task:annotation-driven />

</beans>

java代码中的书写规范:

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

@Service
public class Demo
{
    @Scheduled(cron = "*/5 * * * * ?") 
    public void tast()
    {
        System.out.println("定时任务");
    }
}

 

以上是关于spring-task解决定时问题的主要内容,如果未能解决你的问题,请参考以下文章

Spring任务调度之Spring-Task

Spring任务调度之Spring-Task

Spring任务调度之Spring-Task

基于Spring+SpringMVC+MyBatis开发书评网利用Spring-Task自动计算图书评分以及评论人数

在java中使用ssm框架的定时的实现

Spring注解定时任务