spring3使用task:annotation-driven开始定时

先看一个案例

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");
    }
}
时间: 2024-08-03 15:14:42

spring3使用task:annotation-driven开始定时的相关文章

spring3.0使用annotation完全代替XML(三)

很久之前写过两篇博客: spring3.0使用annotation完全代替XML spring3.0使用annotation完全代替XML(续) 用java config来代替XML,当时还遗留下一些问题: <tx:annotation-driven />声明性事务等配置无法用简单代码来实现 web.xml无法去掉 随着servlet 3.0规范以及spring3.1.M2的发布,现在以上的问题也解决了. 先来说说web.xml,有两种方法来替代 (一)annotation Java代码  

spring3.0使用annotation完全代替XML(续)

从回帖的反应来看,大多数人还是不赞成完全代替XML的,这点倒是在意料之中.我个人还是倾向于用代码来取代XML的Bean定义,当然这更多的是关乎个人偏好,不代表与我观点不同的人就是错的. 先来说说代码相对于XML的优点吧: 1. 更加简洁,相对于XML的"语法",java来得更熟悉 2. 类型安全,更大程度上发挥java静态语言的特性 3. 对重构更加友好 再说说用annotation代替XML的可能性吧,目前看来是不大可能,主要还存在以下的限制: 1. java的语言文化 不知道还有哪

spring3.0使用annotation完全代替XML

@Service与@Component有什么不同?那天被问到这个问题,一时之间却想不起来,就利用这篇文章来纪录spring3.0中常用的annotation. 从spring2.5开始,annotation结合BeanPostProcessor成了扩展Spring IoC容器的常用方法.Spring2.5增加了对JSR-250中@Resource, @PostConstruct, @PreDestroy的支持,Spring 3.0又增加了对JSR-330 (Dependency Injectio

Spring3.0.6定时任务task:scheduled

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/sch

Spring task配置,及解决加载两次的方法

? 关于 启动Task任务同时加载两次的解决方法:? 将spring MVC部分的定义另外建立一个文件,同时把Task配置放在此处,然后在web.xml文件中的处加载 <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-pa

Domain Driven Design and Development In Practice--转载

原文地址:http://www.infoq.com/articles/ddd-in-practice Background Domain Driven Design (DDD) is about mapping business domain concepts into software artifacts. Most of the writings and articles on this topic have been based on Eric Evans' book "Domain Dr

Annotation Type @bean,@Import,@configuration使用--官方文档

@Target(value={METHOD,ANNOTATION_TYPE}) @Retention(value=RUNTIME) @Documented public @interface Bean Indicates that a method produces a bean to be managed by the Spring container. Overview The names and semantics of the attributes to this annotation

Maven3路程(六)用Maven创建Spring3 MVC项目

一.      环境 spring-framework-3.2.4.RELEASE jdk1.7.0_11 Maven3.0.5 eclipse-jee-juno-SR2-win32 二.      创建Web项目 Eclipse中创建Web项目,见前边日志<Maven3路程(三)用Maven创建第一个web项目(1)> 三.      项目依赖,配置pom.xml文件 pom.xml文件如下: <project xmlns="http://maven.apache.org/P

Spring 4 Ehcache Configuration Example with @Cacheable Annotation

http://www.concretepage.com/spring-4/spring-4-ehcache-configuration-example-with-cacheable-annotation Spring 4 Ehcache Configuration Example with @Cacheable Annotation By Arvind Rai, March 16, 2015 In this page, we will learn Spring 4 Ehcache configu