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 的属性注入值的过 ...
随机推荐
- Windows下的lua-5.3.4安装过程
Windows下的lua-5.3.4安装过程 Mingw平台下的编译过程: $ make echo$ make mingw$ make local $ make echo PLAT= none CC= ...
- Good Luck in CET-4 Everybody!
Good Luck in CET-4 Everybody! Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- 分析Array.apply(null, { length: 5 })
Array.apply(null, { length: 5 }) 和 Array(5)有什么不同 注意:ES5,apply函数的第二个参数除了可以是数组外,还可以是类数组对象 // 类转成真正的数组 ...
- JavaScript 和 TypeScript 交叉口 —— 类型定义文件(*.d.ts)
在 <从 JavaScript 到 TypeScript 系列> 文章我们已经学习了 TypeScript 相关的知识. TypeScript 的核心在于静态类型,我们在编写 TS 的时候 ...
- Python爬虫入门:URLError异常处理
大家好,本节在这里主要说的是URLError还有HTTPError,以及对它们的一些处理. 1.URLError 首先解释下URLError可能产生的原因: 网络无连接,即本机无法上网 连接不到特定的 ...
- java非阻塞IO(NIO)流程
单线程 多线程(Netty/Mina)
- 初始MyBatis
初始MyBatis 框架的概念: 框架是一个提供可重复的功用结构的半成品.它为我们构建新的应用程序提供了极大的便利,一方面提供了可以拿来就用的工具,更重要的是提供了可重用的设计.D 框架技术的优势: ...
- 前端如何处理emoji表情
这段时间在做移动端的开发, 有一个功能就是发表评论,其实这个功能本身是比较简单的, 但是在提测是的时候QA给哦提了一个bug,说输入手机自带的emoji表情发送失败了.我就奇怪了,emoji表情也是文 ...
- 浅谈Maven
最近的有用到Maven 所以就大概跟大家谈一下Maven 1什么是Maven? Maven我个人的理解就是项目架包管理 通俗易懂的来说就是管理配置你项目中的所有jar包的 废话不多说 Maven去ht ...
- python 开源全文检索工具 Whoosh
About Whoosh Whoosh is a fast, featureful full-text indexing and searching library implemented in pu ...