Spring通过注解方式实现定时任务

XML配置:

 1 
 2  3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4      xmlns:context="http://www.springframework.org/schema/context"  
 5     xmlns:task="http://www.springframework.org/schema/task" 
 6     xsi:schemaLocation="
 7         http://www.springframework.org/schema/beans 
 8         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
 9         http://www.springframework.org/schema/context 
10         http://www.springframework.org/schema/context/spring-context-3.2.xsd 
11         http://www.springframework.org/schema/task 
12         http://www.springframework.org/schema/task/spring-task-3.2.xsd ">
13     
14     package="com.wisezone" />
15     
16       
17     
18 

java代码:

 1 package com.wisezone.service;
 2 
 3 import java.text.SimpleDateFormat;
 4 import java.util.Date;
 5 
 6 import org.springframework.scheduling.annotation.Scheduled;
 7 import org.springframework.stereotype.Service;
 8 
 9 @Service
10 public class JobService {
11     
12     @Scheduled(cron="0/2 * * * * ?")
13     public void job(){
14         System.out.println("定时输出任务:"+new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
15     }
16 }

测试类:

 1 package com.wisezone.test;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 public class Test
 7 {
 8     public static void main(String[] args)
 9     {
10         ApplicationContext app = new ClassPathXmlApplicationContext("beans.xml");
11         
12     }
13 } 

结果:

Spring通过注解方式实现定时任务_第1张图片

 

转载于:https://www.cnblogs.com/wdh1995/p/6790208.html

你可能感兴趣的:(Spring通过注解方式实现定时任务)