美文网首页
Spring定时任务

Spring定时任务

作者: SmallFleaGood | 来源:发表于2018-09-15 14:42 被阅读0次

①:配置pom.xml

<dependency>

            <groupId>org.quartz-scheduler</groupId>

            <artifactId>quartz</artifactId>

            <version>2.2.3</version>

        </dependency>

②:spring核心配置文件 applicationContext.xml加入配置

头部加入:

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

xsi:http://www.springframework.org/schema/task

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

具体配置加入:

<task:annotation-driven/>

③:在需要定时的方法上加入@Scheduled

@Scheduled(cron = "5 5/30 * * * *")//从第五分钟开始,每三十分钟的第五秒执行一次

public void quartzJobTestMethod() {

        System.out.println("定时任务执行:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));

    }

④:main方法

@EnableScheduling//开启基于注解的定时任务

public class YaKiTimedTask {

private static ApplicationContext context;

public static void main(String[] args) {

context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");

context.getBean(OrderUtils.class);//注入定时任务所在的类

}

}

相关文章

网友评论

      本文标题:Spring定时任务

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