spring自带的定时任务功能,基于注解和xml配置

1、spring的配置文件

[html] view plain copy

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:task="http://www.springframework.org/schema/task"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:aop="http://www.springframework.org/schema/aop"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  9. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  10. http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
  11. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
  12. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  13. http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
  14. <task:annotation-driven /> <!-- 定时器开关-->
  15. <bean id="myTaskXml" class="com.spring.task.MyTaskXml"></bean>
  16. <task:scheduled-tasks>
  17. <!--
  18. 这里表示的是每隔五秒执行一次
  19. -->
  20. <task:scheduled ref="myTaskXml" method="show" cron="*/5 * * * * ?" />
  21. <task:scheduled ref="myTaskXml" method="print" cron="*/10 * * * * ?"/>
  22. </task:scheduled-tasks>
  23. <!-- 自动扫描的包名 -->
  24. <context:component-scan base-package="com.spring.task" />
  25. </beans>

2、基于xml的定时器任务

[java] view plain copy

  1. package com.spring.task;
  2. /**
  3. * 基于xml的定时器
  4. * @author hj
  5. */
  6. public class MyTaskXml {
  7. public void show(){
  8. System.out.println("XMl:is show run");
  9. }
  10. public void print(){
  11. System.out.println("XMl:print run");
  12. }
  13. }

3、基于注解的定时器任务

[java] view plain copy

  1. package com.spring.task;
  2. import org.springframework.scheduling.annotation.Scheduled;
  3. import org.springframework.stereotype.Component;
  4. /**
  5. * 基于注解的定时器
  6. * @author hj
  7. */
  8. @Component
  9. public class MyTaskAnnotation {
  10. /**
  11. * 定时计算。每天凌晨 01:00 执行一次
  12. */
  13. @Scheduled(cron = "0 0 1 * * *")
  14. public void show(){
  15. System.out.println("Annotation:is show run");
  16. }
  17. /**
  18. * 心跳更新。启动时执行一次,之后每隔2秒执行一次
  19. */
  20. @Scheduled(fixedRate = 1000*2)
  21. public void print(){
  22. System.out.println("Annotation:print run");
  23. }
  24. }

4、测试

[java] view plain copy

  1. package com.spring.test;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class Main {
  5. public static void main(String[] args) {
  6. ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-mvc.xml");
  7. }
  8. }

运行结果:
Annotation:print run
Annotation:print run
Annotation:print run
XMl:print run
XMl:is show run
Annotation:print run
Annotation:print run

工程下载地址:http://download.csdn.NET/detail/wxwzy738/7305741

时间: 2024-08-11 21:23:14

spring自带的定时任务功能,基于注解和xml配置的相关文章

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

1 demo package com.test.domi.config; import org.springframework.beans.factory.annotation.Configurable; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springfra

Spring Security 4 Hello World 基于注解 和 XML 例子(带源码)

下一篇: Spring Security 4 自定义登录表单 注解和XML例子 原文:http://websystique.com/spring-security/spring-security-4-hello-world-annotation-xml-example/ [已翻译文章,点击分类里面的spring security 4进行查看] [翻译by 明明如月 QQ 605283073] 本教程演示Spring MVC web项目中Spring Security 4的用法.通过url对访问进

SpringMVC整合Quartz实现定时任务和Spring自带Task定时任务

一.Quartz定时任务1.引入quartz 导入quartz.jar包,或者pom.xml 配置对应的依赖: <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>1.8.6</version> </dependency> 2. Web.xml配置在Web项目web.xml中配

基于注解的Ioc配置 —— Spring(五)

注解配置和Xml配置要实现的功能是一样的,都是要降低程序间的耦合,只是配置的形式不一样. 实际开发中,具体采用注解还是Xml配置,按公司要求. 常用注解: @Component:把资源让Spring来管理,相当于在xml当中配置一个bean. value:指定bean的id,如果不指定value属性,默认bean的id是当前类的类名.首字母小写. @Controller @Service @Repository:都是@Component的衍生注解,作用以及属性都是一样的,只不过提供了更加明确的语

Spring声明式事务管理(基于注解方式实现)

----------------------siwuxie095 Spring 声明式事务管理(基于注解方式实现) 以转账为例 1.导入相关 jar 包(共 10 个包) (1)导入核心 jar 包和日志相关的 jar 包 (2)导入 JdbcTemplate 的 jar 包 (3)导入 MySQL 的 JDBC 驱动包 mysql-connector-java 下载链接: https://dev.mysql.com/downloads/connector/j/ (4)导入 AOP 的 jar

Spring AOP基于注解的“零配置”方式

Spring AOP基于注解的“零配置”方式: Spring的beans.xml中 <!-- 指定自动搜索Bean组件.自动搜索切面类 --> <context:component-scan base-package="org.crazyit.app.service,org.crazyit.app.aspect"> <context:include-filter type="annotation" expression="or

Spring Security 4 自定义登录表单 注解和XML例子(带源码)

上一篇文章: Spring Security 4 Hello World 基于注解 和 XML 例子 下一篇:Spring Security 4 退出 示例 原文地址:http://websystique.com/spring-security/spring-security-4-custom-login-form-annotation-example/ [已翻译文章,点击分类里面的spring security 4查看.] [ 翻译by 明明如月 QQ 605283073] 本文演示Sprin

缓存初解(三)---Spring3.0基于注解的缓存配置+Ehcache和OScache

本文将构建一个普通工程来说明spring注解缓存的使用方式,关于如何在web应用中使用注解缓存,请参见: Spring基于注解的缓存配置--web应用实例 一.简介 在spring的modules包中提供对许多第三方缓存方案的支持,包括: EHCache OSCache(OpenSymphony) JCS GigaSpaces JBoss Cache 等等. 将这些第三方缓存方案配置在spring中很简单,网上有许多介绍,这里只重点介绍如何配置基于注解的缓存配置. 本文将通过例举EHCache和

Struts2基于注解的Action配置

使用注解来配置Action的最大优点就是能够实现零配置,可是事务都是有利有弊的.使用方便.维护起来就没那么方便了. 要使用注解方式,我们必须加入一个额外包:struts2-convention-plugin-2.x.x.jar. 虽说是零配置的,但struts.xml还是少不了的,配置例如以下: <? xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apa