SpringMvc自動任務調度之task實現項目源碼,@Scheduled

1.Xml配置 Spring-job.xml 並在 Spring-Application.xml中Importjava

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd">

    <description>Spring Configuration</description>
    <context:component-scan base-package="包的根目錄例:com.xx.xx"/>
    <!-- 配置任務線性池 -->
    <task:executor  id="executor" pool-size="10" />
    <task:scheduler id="scheduler" pool-size="10"/>
    <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>
    <!-- 若是類文件中沒有使用@Scheduled ,可使用下面的配置 -->
    <!--<task:scheduled-tasks scheduler="scheduler">
        <task:scheduled ref="TestJob" method="test" cron="0/1 * * * * ?"/>
    </task:scheduled-tasks>-->
</beans>

 

2.類文件TASK.javaspring

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

@Component("TestJob")
public class ATask{
    @Scheduled(cron = "0 9 16 * * ?")//每隔5秒隔行一次
    public void test(){
        System.out.println("job1 開始執行。。。。");
    }
}

 

而後就能夠了;spa

原文地址:https://blog.csdn.net/zhulin2012/article/details/51916612.net

相關文章
相關標籤/搜索