Spring通过XML方式实现定时任务

 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     public void job02(){
13         System.out.println("定时输出任务:"+new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
14     }
15 }
 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 } 
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans" 
 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     <context:component-scan base-package="com.wisezone" />
15     <!-- 识别@Scheduled注解 -->
16     <task:annotation-driven/>  
17     
18     <!-- XML方式实现spring定时任务 -->
19     <task:scheduled-tasks>
20         <task:scheduled ref="jobService" method="job02" cron="0/2 * * * * ?"/>
21     </task:scheduled-tasks>
22 </beans>

结果:

 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值