定时任务quartz+spring注解

maven项目用到的依赖

    <dependency>
      <groupId>org.quartz-scheduler</groupId>
      <artifactId>quartz</artifactId>
      <version>2.3.0</version>
    </dependency>
    <dependency>
      <groupId>org.quartz-scheduler</groupId>
      <artifactId>quartz-jobs</artifactId>
      <version>2.3.0</version>
    </dependency>

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:task="http://www.springframework.org/schema/task"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/task
        http://www.springframework.org/schema/task/spring-task-4.0.xsd
        http://www.springframework.org/schema/context
	    http://www.springframework.org/schema/context/spring-context-4.2.xsd"
        >
    
    <!--启用注解,加载定时任务-->
    <task:annotation-driven />
    <context:annotation-config />
    <!--定时任务扫描的包-->
 <context:component-scan base-package="com.hezhan.develop.web.quartz"/>
</beans>

执行任务的java文件

 
 
@Component
public class QuartzJob {

    /**
     * 每分钟执行一次
     */
    @Scheduled(cron = "0 0/1 * * * ?")
    public void execute() {

        System.out.println("=========定时任务开始==============");
        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) );
        System.out.println("=========定时任务结束==============");

    }

}

web.xml引入spring的配置文件,略

项目启动,成功







猜你喜欢

转载自blog.csdn.net/qiye_zhou/article/details/80524474