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 的属性注入值的过 ...
随机推荐
- Leetcode题解(六)
21.Merge Two Sorted Lists 题目 直接上代码: class Solution { public: ListNode *mergeTwoLists(ListNode *l1, L ...
- code force 403B.B. The Meeting Place Cannot Be Changed
B. The Meeting Place Cannot Be Changed time limit per test 5 seconds memory limit per test 256 megab ...
- 2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest A Email Aliases(模拟STL vector+map)
Email AliasesCrawling in process... Crawling failed Time Limit:2000MS Memory Limit:524288KB ...
- 单元测试 Qunit
http://api.qunitjs.com/category/assert/ 测试方法 选中 "Check for Globals" 会暴露全局对象,看你的代码会不会无 ...
- vue.js项目安装
Vue.js 安装 NPM 方法安装vue.js项目 npm 版本需要大于 3.0,如果低于此版本需要升级它: # 查看版本 $ npm -v 2.3.0 #升级 npm npm install np ...
- C#中制作MDI窗体
在VB中做 MDI窗体很简单.在C#里就没有这个轻松了,不过还是很方便的. 首先在C#里添加一个窗体,命名为MdiMain,将其IsMdiContainer设定成true,这样MDI主窗体就建立了.然 ...
- Win32 SDK 编程开始, 创建窗口, 消息的处理, 消息循环
Windows SDK 编程的一般步骤为: 1. 注册窗口类, 使用到的结构 WNDCLASSEX, 函数 RegisterClassEx. 2. 创建窗口, 函数 CreateWindowEx. 3 ...
- [转载] ZooKeeper的Java客户端API
转载自 http://www.cnblogs.com/ggjucheng/p/3370359.html http://zookeeper.apache.org/doc/trunk/javaExampl ...
- Python的路径引用
1.以HOME目录为准,进行跳转 sys.path.append(os.path.dirname(__file__) + os.sep + '../') from config import swor ...
- Spring JDBC 示例
在使用普通的 JDBC 数据库时,就会很麻烦的写不必要的代码来处理异常,打开和关闭数据库连接等.但 Spring JDBC 框架负责所有的低层细节,从开始打开连接,准备和执行 SQL 语句,处理异常, ...