spring3使用task:annotation-driven开始定时
2015-07-05 13:39
681 查看
先看一个案例
spring配置文件如下:
测试类
package com.jCuckoo.demo; import java.text.SimpleDateFormat; import java.util.Date; import org.springframework.scheduling.annotation.Scheduled; public class Singer { //@Scheduled(fixedDelay=1000) //第一种方式 //fixedDelay延时多少毫秒,多少毫秒执行一次 /* 1 Seconds (0-59) 2 Minutes (0-59) 3 Hours (0-23) 4 Day of month (1-31) 5 Month (1-12 or JAN-DEC) 6 Day of week (1-7 or SUN-SAT) 7 Year (1970-2099) 取值:可以是单个值,如6; 也可以是个范围,如9-12; 也可以是个列表,如9,11,13 也可以是任意取值,使用* */ //0 * * * * * 代表每分钟执行一次 @Scheduled(cron="0 * * * * *") //第二种方式 public void singing(){ Date date=new Date(); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); System.out.println(sdf.format(date)); } }
spring配置文件如下:
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd "> <task:annotation-driven /> </beans>
测试类
package com.jCuckoo.demo; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainTest { public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); Singer singer=(Singer) context.getBean("singer"); } }
相关文章推荐
- Why am I getting an Unreachable Statement error in Java?
- 在MyEclipse中统计项目行数
- Spring Resource接口
- JAVA实验第一天
- 在MyEclipse中统计项目行数
- 《java课程设计》之猜猜看游戏(一)
- java里面包的重要性-管理类文件
- Eclipse快捷键 10个最有用的快捷键
- Java类编译后Class文件概述(下)
- java反射:使用asm 获取方法的参数名
- Java基础(六)
- Java-this
- Java-构造函数
- Java-封装基础
- Java环境配置_JDK安装及配置环境变量
- Java-匿名对象
- JAVA实验第四天
- Java-成员变量和局部变量
- Java-面向对象概念
- java 线程池的作用