Spring任务调度之SpringTask基于XML和基于注解的使用示例
使用Spring的环境要求是:JDK1.8以上、Maven3.0以上。
Maven依赖
SpringTask集成在SpringContext中,所以只需要SpringContext即可。
可以使用maven-compiler-plugin显式的指定JDK版本。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
Spring xml配置(基于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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd">
<!--配置注解扫描-->
<context:component-scan base-package="com.cky.schadule"/>
<task:scheduler id="taskScheduler" pool-size="100" />
<task:scheduled-tasks scheduler="taskScheduler">
<!--每30s触发一次-->
<task:scheduled ref="schaduleDemo" method="sayHello1" cron="0/5 * * * * ?"/>
<!--每10s触发一次-->
<task:scheduled ref="schaduleDemo" method="sayHello2" cron="0/10 * * * * ?"/>
</task:scheduled-tasks>
</beans>
任务类
@Component
public class SchaduleDemo {
public void sayHello1(){
System.out.println("task1:hello~ now time is"+ LocalTime.now());
}
public void sayHello2(){
System.out.println("task2:hello~ now time is0"+ LocalTime.now());
} public static void main(String[] args){
ApplicationContext context=new ClassPathXmlApplicationContext("classpath:/application-context.xml");
}
}
Spring.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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd">
<!--配置注解扫描-->
<context:component-scan base-package="com.cky.schadule"/>
<task:scheduler id="taskScheduler" pool-size="100" />
<!--开启注解驱动-->
<task:annotation-driven scheduler="taskScheduler"/>
</beans>
任务类
@Component
public class SchaduleDemo {
@Scheduled(cron = "0/5 * * * * ?")
public void sayHello1(){
System.out.println("task1:hello~ now time is"+ LocalTime.now());
}
@Scheduled(cron = "0/10 * * * * ?")
public void sayHello2(){
System.out.println("task2:hello~ now time is0"+ LocalTime.now());
} public static void main(String[] args){
ApplicationContext context=new ClassPathXmlApplicationContext("classpath:/application-context.xml");
} }
Spring任务调度之SpringTask基于XML和基于注解的使用示例的更多相关文章
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring使用AspectJ开发AOP基于XML和基于Annotation
AspectJ 是一个基于 Java 语言的 AOP 框架,它扩展了 Java 语言.Spring 2.0 以后,新增了对 AspectJ 方式的支持,新版本的 Spring 框架,建议使用 Aspe ...
- 关于什么是SpringMVC,和SpringMVC基于xml配置、注解配置、纯注解配置
首先我们先要了解一下,什么是SpringMVC? SpringMVC是Spring框架内置的MVC的实现.SpringMVC就是一个Spring内置的MVC子框架,也就是说SpringMVC的相关包都 ...
- AspectJ基于xml和基于注解
一.基于xml 执行的切入点中具体方法有返回值,则方法结束会立即执行后置通知,然后再执行环绕通知的放行之后的代码: 2.连接点即所有可能的方法,切入点是正真被切的方法,连接点方法名: 其中,只有环绕通 ...
- Spring深入浅出(三)XML方式以及注解的方式操作IOC
在日常的开发过程中,我们把程序分为3层:Controller层,Service层,DAO层.Controller类似于Servlet,也就是MVC中的控制层. 调用的顺序是: Controller层调 ...
- Spring任务调度之Spring-Task
一.前言 上面两篇介绍了在Spring 中使用Timer与Quartz,本篇将介绍Spring3.0以后自主开发的定时任务工具,spring task,可以将它比作一个轻量级的Quartz,而且使用起 ...
- MyBatis 项目开发中是基于 XML 还是注解?
只要你对 MyBatis 有所认识和了解,想必知道 MyBatis 有两种 SQL 语句映射模式,一种是基于注解,一种是基于XML. 基于 XML <mapper namespace=" ...
- springmvc——基于xml的异常映射和基于注解的异常映射
SpringMVC提供了基于XML和基于注解两种异常映射机制.这两种异常映射不能够只使用一个,他们需要一起使用.因为有些异常是基于注解异常映射捕获不到的. 在springmvc中,一个请求如果是由&l ...
- step6----->往工程中添加spring boot项目------->修改pom.xml使得我的project是基于spring boot的,而非直接基于spring framework
文章内容概述: spring项目组其实有多个projects,如spring IO platform用于管理external dependencies的版本,通过定义BOM(bill of mater ...
- Spring基础——在 Spring Config 文件中基于 XML 的 Bean 的自动装配
一.Spring IOC 容器支持自动装配 Bean,所谓自动装配是指,不需要通过 <property> 或 <constructor-arg> 为 Bean 的属性注入值的过 ...
随机推荐
- 异常详细信息: Abp.AbpException: No language defined!
程序运行后,出现错误:No language defined! 解决方法: 1.检查是否已创建数据库,若未创建则在程序包管理控制台执行命令:Update-Database 2.检查表AbpLangua ...
- SpringMVC , Spring , MyBatis 文件上传
学习一下文件上传下载,为图片上传做准备,感觉有一个世纪没玩过上传下载了,边敲代码边记录,请各路大神指教: 参考:http://blog.csdn.net/wjycgl/article/details/ ...
- JAVA 通过 Socket 实现 TCP 编程
简介 TCP简介 TCP(Transmission Control Protocol 传输控制协议)是一种面向连接的.可靠的.基于字节流的传输层通信协议,由IETF的RFC 793定义.在简化的计算机 ...
- linq 在查询表达式中处理 null 值
此示例显示如何在源集合中处理可能的 null 值. IEnumerable<T> 等对象集合可包含值为 null 的元素. 如果源集合为 null 或包含值为 null 的元素,并且查询不 ...
- js判断对象还是数组
1.对于Javascript 1.8.5(ECMAScript 5),变量名字.isArray( )可以实现这个目的 var a=[]; var b={}; Array.isArray(a);//tr ...
- strict 严格模式
严格模式可以让你更早的发现错误,因为那些容易让程序出错的地方会被找出来 打开严格模式:"use strict" 不支持的javascript引擎会忽略它,当作是一个未赋值字符串 ...
- Python文件夹备份
Python文件夹备份 import os,shutil def file_copy(path1,path2): f2 = [filename1 for filename1 in os.listdir ...
- 外键删除(T-SQL Drop Foreign Key)
列出某张表相关的 FK Name select distinct name from sys.objects where object_id in ( select fk.constraint_o ...
- Dev控件 galleryControl
发现一个规律,不会的控件先拖到界面上,右上角需要add 的就对应add一个.然后就是找属性和集合手动添加几个. 然后把XXXForm.Designer.cs 里面的代码提取到逻辑代码中,就把常量换成变 ...
- 谈谈我的移动端rem适配方案
最近有点怀疑人生,毕竟一个人写前端,有时候会怀疑自己理解的一些东西包括用法有没有符合标准.趁着这阵子闲下来,翻了翻别人的rem适配博客,发现有点绕口,怪自己是个强迫症,啥都要自己去试试结果并从中理解, ...