美文网首页
Spring Schedule

Spring Schedule

作者: Roct | 来源:发表于2019-10-15 22:21 被阅读0次

Cron表达式快速入门

常用表达式 介绍
0 0 0 * * ? 每天0点一次
0 0 23 * * ? 每天23点一次
0 */1 * * * ? 每1分钟执行一次(每个1分钟的整数倍)
0 0 */6 * * ? 每6个小时执行一次(每个6小时的整数倍)
0 0 */1 * * ? 每1个小时执行一次(每个1小时的整数倍)

Spring Schedule Cron配置

  • @Component
  • @Scheduled
  • @Scheduled(cron="0 * /1 * * * ? ")

Spring Schedule配置文件设置

<?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:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
     http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--扫描com.mmall下的注解-->
    <!--<context:component-scan base-package="com.mmall" annotation-config="true"/>-->

    <context:component-scan base-package="com.mmall" annotation-config="true">
        <context:exclude-filter type="annotation"
                                expression="org.springframework.stereotype.Controller" />
    </context:component-scan>

    <!--  spring-schedule  -->
    <task:annotation-driven />
    <context:property-placeholder location="classpath:datasource.properties"/>


    <!--通过配置织入@Aspectj切面-->
    <aop:aspectj-autoproxy/>

    <!--分离出一个applicationContext-datasource.xml-->
    <import resource="applicationContext-datasource.xml"/>
    <import resource="applicationContext-session.xml"/>
</beans>

创建task package /egTask

@Component
public class CloseOrderTask {
    @Scheduled(cron = "0 */1 * * * ?") // 每1分钟执行一次
    public void test() {
        System.out.println("test spring schedule");
    }
}

启动tomcat, 即可实现定时任务.

相关文章

网友评论

      本文标题:Spring Schedule

      本文链接:https://www.haomeiwen.com/subject/ytpsmctx.html