springboot让开发更简单!springmvc中启用定时任务还得需要在xml中进行配置启用并且要配置扫描器,但是springboot只需要一个注解就可以。

@EnableScheduling

无需多余的jar依赖,所以pom不贴了

applaction.java

  1. package com.sbm;
  2.  
  3.  import org.springframework.boot.SpringApplication;
  4.  import org.springframework.boot.autoconfigure.SpringBootApplication;
  5.  import org.springframework.scheduling.annotation.EnableScheduling;
  6.   
  7.  @SpringBootApplication
  8.  @EnableScheduling
  9.  public class Application {
  10.   
  11.  public static void main(String[] args) {
  12.  SpringApplication.run(Application.class, args);
  13.  }
  14.  }

定时任务类AppCoreTask.java

  1.  package com.sbm.scheduling;
  2.   
  3.  import org.springframework.scheduling.annotation.Scheduled;
  4.  import org.springframework.stereotype.Component;
  5.  import java.util.Date;
  6.   
  7.  @Component
  8.  public class AppCoreTask {
  9.   
  10.  @Scheduled(cron = "0 53 15 * * ? ")
  11.  public void tesk() {
  12.  System.out.print("开启定时任务" + new Date());
  13.  }
  14.  }

要求在15:53分打印一句话

关于cron表达式的用法,可以在我的这篇文章里查看:Spring Task 中cron表达式整理记录

运行sb程序


  1.  
    016-12-30 15:52:24.653 INFO 4204 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
  2.  
    2016-12-30 15:52:24.654 INFO 4204 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
  3.  
    2016-12-30 15:52:24.722 INFO 4204 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
  4.  
    2016-12-30 15:52:25.183 INFO 4204 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
  5.  
    2016-12-30 15:52:25.212 INFO 4204 --- [ main] s.a.ScheduledAnnotationBeanPostProcessor : No TaskScheduler/ScheduledExecutorService bean found for scheduled processing
  6.  
    2016-12-30 15:52:25.315 INFO 4204 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
  7.  
    2016-12-30 15:52:25.326 INFO 4204 --- [ main] com.sbm.Application : Started Application in 7.124 seconds (JVM running for 8.234)
  8.  
    开启定时任务Fri Dec 30 15:53:00 CST 2016

spingboot @EnableScheduling的更多相关文章

  1. vue打包发布在spingboot项目中 vue-router路由的处理

    (原) 以下例子springboot后端地址为:localhost:7080/pingandai vue前端地址为:locahost:8080/pingandai/ 1.如果路由模式设置的是histo ...

  2. @EnableScheduling注解

    @EnableScheduling 开启对定时任务的支持       其中Scheduled注解中有以下几个参数: 1.cron是设置定时执行的表达式,如 0 0/5 * * * ?每隔五分钟执行一次 ...

  3. Spring boot @EnableScheduling 和 @Scheduled 注解使用例子

    前言 Spring Boot提供了@EnableScheduling和@Scheduled注解,用于支持定时任务的执行,那么接下来就让我们学习下如何使用吧: 假设我们需要每隔10秒执行一个任务,那么我 ...

  4. SpringBoot系列——EnableScheduling,对定时器的支持

    前言 定时器功能在项目里面往往会用到,比如定时发送邮件.定时释放数据库资源:这里记录一下springboot对定时器的支持的简单实例 cron表达式 开始之前要先介绍一下cron表达式,这里当一下百度 ...

  5. spingBoot整合mybatis+generator+pageHelper

    spingBoot整合mybatis+generator+pageHelper 环境/版本一览: 开发工具:Intellij IDEA 2018.1.4 springboot: 2.0.4.RELEA ...

  6. spring自带的定时任务功能@EnableScheduling

    1 demo package com.test.domi.config; import org.springframework.beans.factory.annotation.Configurabl ...

  7. Spring注解之 @EnableScheduling计划任务注解

    要实现计划任务,首先通过在配置类注解@EnableScheduling来开启对计划任务的支持, 然后在要执行计划任务的方法上注解@Scheduled,声明这是一个计划任务 示例:计划任务执行类 在这个 ...

  8. SpingBoot的认识和基本使用

    认识SpingBoot Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的初始搭建以及开发过程. -使用springboot以后,搭建一个spring应 ...

  9. spingboot集成jpa(一)

    springboot + jpa 练习 spingboot集成jpa(一):最基本的环境搭建 spingboot集成jpa(二):使用单元测试 1. pom.xml中添加依赖 <!-- jdbc ...

随机推荐

  1. 42.纯 CSS 创作一个均衡器 loader 动画

    原文地址: https://segmentfault.com/a/1190000015157160 感想: 不难,最简单的动画.拓展地址: https://scrimba.com/c/cWqVv9hd ...

  2. Redis、MongoDB及Memcached的区别

    Redis(内存数据库) 是一个key-value存储系统(布式内缓存,高性能的key-value数据库).和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).l ...

  3. Hive 优化汇总

    参考: http://www.cnblogs.com/yshb/p/3147710.html http://www.cnblogs.com/sandbank/p/6408762.html 一   jo ...

  4. 关于 build tools

    1.build tools是什么 Build Tools 即构建工具是一个把源代码生成可执行应用程序的过程自动化的程序(例如Android app生成apk).构建包括编译.连接跟把代码打包成可用的或 ...

  5. js人形时钟

    https://blog.csdn.net/rsylqc/article/details/44808063 分享自:http://chabudai.org/blog/?p=59 在这个网站看到一个很有 ...

  6. Linux系统基础5周入门精讲(Linux发展过程)

    什么是操作系统 作为应用开发程序员,我们开发的软件都是应用软件,而应用软件必须运行于操作系统之上,操作系统则运行于硬件之上,应用软件是无法直接操作硬件的,应用软件对硬件的操作必须调用操作系统的接口,由 ...

  7. self, super理解

    self是方法参数列表中的第一个参数,是运行时决定的. super是编译器符号,是编译时决定的.super的含义为从父类开始寻找相应的方法,父类在编译的时候就已经决定了. 一个关键点:super并不代 ...

  8. EventBus 源码学习

    打开一看,原来相关代码并不多,下面看下细节 主要方法也就是注册,取消注册和发送事件,可以看到两个主要的变量就是subscribers和dispatcher public void register(O ...

  9. python反汇编函数字节码

    使用dis模块 >>> def test(): ... print(1) ... a = 1 ... print(a) ... >>> from dis impor ...

  10. jquery 设计的扩展---初级

    1. 写一个构造函数G,调用G 时,返回G上的fn 对象的init() 的实例 2.设置G.fn 的指向,使用G.fn 与G.prototype指向同一个对象 2.1 重写G.prototype 对象 ...