不使用quartz实现定时,使用@Scheduled注解实现定时--一次配置到处使用

来源:互联网 发布:知之一字 众妙之门 编辑:程序博客网 时间:2024/06/06 03:43

在spring中我们可以用task:executor 实现定时执行任务,可以大大的减少像quartz这样的配置,quartz配置很麻烦,首先要配置我们的spring-content.xml 在xmlns 多加下面的语句

xmlns:task="http://www.springframework.org/schema/task"  

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

http://www.springframework.org/schema/task  http://www.springframework.org/schema/task/spring-task-3.1.xsd  

整个约束是:

<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: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-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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd"    default-lazy-init="true">

在内容中配置:

 <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(cron="0 0 0 * * ? *")   //每天0点定时执行    public void test(){        System.out.println("run as  ............");    }

ok码完收工

0 0
原创粉丝点击