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 的属性注入值的过 ...
随机推荐
- Host文件简介
摘抄自:http://www.cnblogs.com/zgx/archive/2009/03/10/1408017.html.百度百科:hosts文件 很奇怪有很多人不知道Hosts是什么东西.在网络 ...
- J - Scarily interesting! URAL - 2021
This year at Monsters University it is decided to arrange Scare Games. At the Games all campus gathe ...
- PHP基础入门(二)【PHP函数基础】
PHP基础入门(二)--函数基础 了解 PHP基础入门详解(一) 后,给大家分享一下PHP的函数基础. 这部分主要讲的就是: 函数的声明与使用.PHP中变量的作用域.静态变量.函数的参数传递.变量函数 ...
- 用iptables 做NAT代理上网
背景:有一台A服务器不能上网,和B服务器通过内网来连接,B服务器可以上网,要实现A服务器也可以上网. 内网主机: A eth1:172.16.1.8 外网主机: B eth0:10.0.0.61外网主 ...
- sql脚本
Windows下执行命令 \. d:\book.sql 这里使用了case when 这个小技巧来实现批量更新.//一个字段 UPDATE categories SET display_or ...
- Java IO编程全解(三)——伪异步IO编程
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/7723174.html 前面讲到:Java IO编程全解(二)--传统的BIO编程 为了解决同步阻塞I/O面临 ...
- Android 开发笔记___alertDialog
public class AlertActivity extends AppCompatActivity implements OnClickListener { private TextView t ...
- php图片上传服务器
原理是把图片上传到服务器的某个目录,然后在把他的名字存入数据库,或者不需要数据库这部分也行.读取的时候直接读取名字. HTML提交表格 <form method="post" ...
- eclipse导出jar(含依赖)三步走
之前用eclipse导出jar运行结果一直不尽人意,排查问题排查很久,最终确定到导出jar时,如果依赖jdk以外的jar时,就要通知eclipse 看了很多帖子,感觉操作起来都比较麻烦,注意点也比较多 ...
- Vue.js优雅的实现列表清单
一.Vue.js简要说明 Vue.js (读音 /vjuː/) 是一套构建用户界面的渐进式框架.与前端框架Angular一样, Vue.js在设计上采用MVVM模式,当View视图层发生变化时 ...