实现Spring定时任务

实现过程

  1. 在spring-servlet.xml文件中引入task的命名空间;
        xmlns:task="http://www.springframework.org/schema/task"
        xsi:schemaLocation="http://www.springframework.org/schema/task
        http://www.springframework.org/schema/task/spring-task-4.0.xsd"
    
  2. 配置task的扫描注解;
       <task:annotation-driven scheduler="qbScheduler"  mode="proxy" />
       <task:scheduler id="qbScheduler" />
    
  3. 使用@Scheduled(cron = “时间格式串”) 实现定时任务;
       @Scheduled(cron = "* * * * * ?")//每分钟都执行一次
         public void test(){
          System.out.println("执行");
        }
    

cron 参数设置

    一个cron表达式有至少6个(也可能7个)有空格分隔的时间元素。
    按顺序依次为
    秒(0~59)
    分钟(0~59)
    小时(0~23)
    天(月)(0~31,但是你需要考虑你月的天数)
    月(0~11)
    天(星期)(1~7 1=SUN 或 SUN,MON,TUE,WED,THU,FRI,SAT)
    年份(1970-2099)
    "0/5 * * * * ?"         每5秒一次触发 
    "0 0 8 * * ?"           每天早晨8点触发 
    "0 30 12 ? * *"         每天早中午12:30触发 
    "0 0/5 14,18 * * ?"     每天的下午2点至2:55和6点至6点55分两个时间段内每5分钟一次触发 

spring-servlet.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:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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.0.xsd"
	xmlns:task="http://www.springframework.org/schema/task">
	<!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
	<context:component-scan base-package="com.bmw.hdms"
		use-default-filters="false">
		<context:include-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
		<context:include-filter type="annotation"
			expression="org.springframework.web.bind.annotation.ControllerAdvice" />
	</context:component-scan>
	<!-- 定义跳转的文件的前后缀 ,视图模式配置 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/pages/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	<bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
		<property name="order" value="100"></property>
	</bean>
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="defaultEncoding" value="utf-8"></property>
		<property name="maxUploadSize" value="10485760000"></property>
		<property name="maxInMemorySize" value="40960"></property>
	</bean>

	<mvc:default-servlet-handler />
	<mvc:annotation-driven />

	<mvc:view-controller path="" view-name="home/index" />
	<mvc:view-controller path="/index" view-name="home/index" />
	<mvc:view-controller path="/welcome" view-name="home/welcome" />

	<bean id="messageSource"
		class="org.springframework.context.support.ResourceBundleMessageSource">
		<property name="basename" value="i18n"></property>
	</bean>
	<!-- 定时任务 -->
	<task:annotation-driven scheduler="qbScheduler"
		mode="proxy" />
	<task:scheduler id="qbScheduler" />

</beans>

java代码展示

代码必须放在Controller层,不然Spring可能检测不到

 @Scheduled(cron = "0/5 * * * * ?")  //每隔5秒执行一次定时任务
	    public void consoleInfo(){
	        System.out.println("定时任务"+new Date());
	   }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值