SSM项目加入定时任务task

哈哈哈哈,花费我一早上的时间,不断的百度、测试、百度、测试…终于让我学会了使用方法啊,感谢各位大大
在这里插入图片描述

ps:这个定时器是基于SSM框架下搭建的啊,有些配置文件我可能放的位置跟大家不太一样哈,详细的可参考我的这篇博客:https://blog.csdn.net/kuguhuan/article/details/107455891

好的废话结束,正文开始

1,新建spring-task.xml文件

<?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:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/task
        http://www.springframework.org/schema/task/spring-task-3.2.xsd">
    <!-- 设置定时任务 -->
    <task:annotation-driven/>
	<!-- base-package放的是你要定时的那个类所在的包路径-->
    <context:component-scan base-package="com.pojo"></context:component-scan>
</beans>

2,在applicationContext.xml文件中,添加spring-task.xml文件
在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- spring的核心配置文件-->

    <import resource="spring-dao.xml"/>
    <import resource="spring-service.xml"/>
    <import resource="spring-mvc.xml"/>
    <import resource="spring-task.xml"/>
</beans>

3,在com.pojo文件夹下新建Test定时任务类

package com.pojo;

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

import java.text.SimpleDateFormat;
import java.util.Date;
@Controller
public class Test {

//    DESC :  编写定时任务,每5秒输出一次
    @Scheduled(cron = "*/5 * * * * ?")
    public void run(){
        Date dd=new Date();
        //格式化
        SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time=sim.format(dd);
        System.out.println(time + "现在执行了run");
    }

}

注意:class类上要加@Controller,定时任务的方法上要加入@Scheduled

如果定时任务需要注入Service层对数据库进行操作,例如:

@Autowired
@Qualifier("InsuranceServiceImpl")
private InsuranceService insuranceService;

这里就涉及一个问题:

因为是在SSM框架下的定时任务,在多线程时使用@Autowired总是获取不到bean,insuranceService会变为null

原因是:new thread不在spring容器中,也就无法获得spring中的bean对象。

要解决这个,最简单的方法是手动获取

//单独处理autowaire注入无效
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
insuranceService = (InsuranceService) ac.getBean("InsuranceServiceImpl");

4,启动Tomcat,就可以看到控制台输出的变化

悄咪咪的说,比那个什么劳什子quartz好用多了

补充:有关触发器时间表达式corn配置

每隔5秒执行一次任务:  "*/5 * * * * ?"
 
每隔1分钟执行一次任务:  "0 */1 * * * ?"
 
每天23点执行一次任务:  "0 0 23 * * ?"
 
每天凌晨1点执行一次任务:  "0 0 1 * * ?"
 
每月1号凌晨1点执行一次任务:  "0 0 1 1 * ?"
 
每月1号凌晨2点执行一次任务:  "0 0 2 1 * ? *"
 
每月最后一天23点执行一次任务:  "0 0 23 L * ?"
 
每周星期天凌晨1点执行一次任务:  "0 0 1 ? * L"
 
26分、29分、33分各执行一次任务:  "0 26,29,33 * * * ?"
 
每天的0点、13点、18点、21点各执行一次任务:   "0 0 0,13,18,21 * * ?"
 
周一到周五每天上午10:15执行一次任务:  "0 15 10 ? * MON-FRI" 
 
2020-2021年的每个月的最后一个星期五上午10:15执行一次任务: "0 15 10 ? 6L 2020-2021"
 
 
 
#在线配置
https://qqe2.com/cron
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值