`
kinfer
  • 浏览: 7612 次
社区版块
存档分类
最新评论

Spring定时器注解

阅读更多

Spring注解已经用了一段时间,但是在实现定时器的时候还是遇到了一些问题,主要还是XML的问题,没有引入spring task的schema;这里记录一下。
Spring的定时注解主要分两步:

1. 创建task的xml,引入spring的task schema,并配置好task所在的package,spring会自动扫描该package下所有注解的类和注解的方法;

<?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:tx="http://www.springframework.org/schema/tx"
    xmlns:task="http://www.springframework.org/schema/task"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/tx
         http://www.springframework.org/schema/tx/spring-tx.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/task
         http://www.springframework.org/schema/task/spring-task-3.0.xsd">
		
	<context:component-scan base-package="com.xx.schedule" />
	<task:annotation-driven/>
</beans>

 

 2. 创建相应的task类,为方法设置@Scheduled

package com.xx.schedule;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
 * 计息定时器
 * @author kinfer
 *
 */
@Component
public class WorkerScheduler {
	private static final Logger log = LoggerFactory.getLogger(WorkerScheduler.class);
	@Autowired
	private InterestCalculationWorker interestCalcWorker;
	
	@Scheduled(cron="59 30 23 * * ?")
	public void scheduleInterestCalc(){
		
		log.debug("schedule to calc checking acct interest");
		interestCalcWorker.calcCheckingAcctInterest();
	}

}

 这里的@Scheduled(cron="59 30 23 * * ?") 表示每天的23:30:59执行,cron表达式可以到网上找相关的资料。

 

分享到:
评论

相关推荐

    spring 定时器 注解+xml 方式模板

    spring schedule 注解方式+xml 方式 实现的定时器功能

    spring定时器代码注释

    spring定时器 &lt;!-- 第一步 声明一个定时任务,注意不是直接声明,而是声明一个JobDetailBean,通过jobClass属性设置一个定时对象 --&gt; .....................下载再看

    spring定时器的mxl和注解配置

    一个maven工程例子,包括spring整合task定时任务,分为xml方式,注解方式,注解线程池三种方式;

    Spring boot 实现定时器.docx

    只需要几个注解就能实现定时器,轻松无压力!!!!!如有不懂的地方可以找我

    老外的 spring scheduler 实例 使用注解,比较全比较实用

    老外的 spring scheduler 实例 使用注解,比较全比较实用

    subscribe:定时获取关注者信息

    修改 SubscribeServiceImpl 下的doWork方法里的spring定时器注解时间配法即可 修改数据库 databse.propertics 里修改数据库配置 修改微信 wechat.propertics 修改执行的类型 (测试或者正式) 打包 通过maven执行...

    springjdbc

    -- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --&gt; &lt;bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /&gt; &lt;!-- apache.dbcp连接池的配置 --&gt; ...

    springCloud

    4)启动 cloud-provide 此处swagger-ui,定时器,自定义拦截器 功能 5)启动 cloud-consumber 此处有Feign,hystrix功能 此时访问http://127.0.0.1:9000/hello 可获取到结果 6)启动 cloud-getaway 此处有zuul拦截...

    springCloud.rar(私聊博主要密码)

    + springBoot + springCloud + 日志组件logback-spring + 多配置 + 多数据源...+ 传参注解式校验 + session练习 + 公用日志设计封装 + db乐观锁设计 + 优雅启停 + 配置文件信息加密 + AES加解密 + spring 事件监听设计

    springMvc定时器执行两次

    NULL 博文链接:https://ch-dj.iteye.com/blog/2186072

    SpringBoot集成常用开发中间件,分库分表,缓存,消息队列,定时器,权限管理等组件.zip

    2,使编码变得简单,SpringBoot采用 JavaConfig的方式对Spring进行配置,并且提供了大量的注解,极大的提高了工作效率,比如@Configuration和@bean注解结合,基于@Configuration完成类扫描,基于@bean注解把返回值...

    Spring定时任务(多个实例)

    Spring定时任务,里面有多个实例,配置形式的 还有注解实现的,还有quartz和Spring实现的,希望对大家有用

    quartz:关于定时器独立使用,和与spring 结合使用,本项目使用注解注入

    quartz 和 spring 结合使用的一个demo 简单注入,有参任务 和 无参 任务的介绍相同bean 如何 处理。要用 Qualifier 指定哪个bean注入。里面还有quartz独自使用的demo。在test里。遇见异常 和 异常回滚 任务, 如何...

    详解Spring Boot中使用@Scheduled创建定时任务

    本篇文章中主要介绍了Spring Boot中使用@Scheduled创建定时任务,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。

    SSM项目整合案例

    简单SSM项目整合,用SSM框架实现用户表的增,删,改 ,查,用到AOP面向切面事务,事务 , 以及事务通知,SpringMVC定时器 ,dataSource配置,注解。

    Java开发的一个简约后台管理系统源码.zip

    项目简介:Java开发的一个简约后台管理系统 1.需求定义:外包项目如雨后春笋,开发工期...1.控制层接口版本管理、包装响应Result返回值进行全局处理,使用@ResponseResultBody注解 2.定义接口版本,在方法中配置@ApiVe

    面试准备1

    2、技术问题1、定时器的使用1、springboot @Scheduled注解2、quartz,主要是通过和spring整合后使用,需要创建专门的配置文件spr

Global site tag (gtag.js) - Google Analytics