spring task 定时任务实现示例

网友投稿 188 2023-06-17


spring task 定时任务实现示例

一、引入spring相关jar包:

二、在web.xml中配置spring

Spring监听器

org.springframework.web.context.ContextLoaderListener

contextConfigLocation

classpath:applicationContext.xml

三、在applicationContext.xml中配置监听器

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

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

xsi:schemaLocation="http://springframework.org/schemhttp://a/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.0.xsd

http://springframework.org/schema/task

http://springframework.org/schema/task/spring-task-3.0.xsd"

default-lazy-init="false">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

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

xsi:schemaLocation="http://springframework.org/schemhttp://a/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.0.xsd

http://springframework.org/schema/task

http://springframework.org/schema/task/spring-task-3.0.xsd"

default-lazy-init="false">

四、编写实体类

package com.test.task;

import java.text.DateFormat;

import java.util.Date;

import org.springframework.scheduling.annotation.Scheduled;

import org.springframework.stereotype.Component;

@Component

public class TestTask {

@Scheduled(cron = "*/5 * * * * ?")

public void print(){

String time = DateFormat.getDateTimeInstance().format(new Date());

System.out.println("定时器触发打印"+time);

}

}

五、工程目录:

运行结果:


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:JAVA 多线程之信号量(Semaphore)实例详解
下一篇:Java实现时间日期格式转换示例
相关文章

 发表评论

暂时没有评论,来抢沙发吧~