Spring 定时器注解 与配置文件

10 篇文章 0 订阅


定时器 配置 方式 1

配置文件



	<bean id="zgGISJob" class="com.jt.zgsic.scheduler.ZGGISJob" />
	<task:scheduled-tasks>
		<task:scheduled ref="zgGISJob" method="gisRun" cron="0 */1 * * * ?" />
	</task:scheduled-tasks>


java 代码

@Component
public class ZGGISJob {
	private static Logger log = Logger.getLogger(ZGGISJob.class);
	
	@Autowired
	SysAreaService sysAreaService;
	
	public void gisRun(){
		
		System.out.println("hello!");
		//SysArea record=new SysArea();
		//sysAreaService.selectByEntity(record);
	}
	
}






定时器配置方式2 


(1):修改配置 文件 如下:


[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <!-- 注解扫描包 -->  
  2. <task:annotation-driven/><!-- 用定时器注解  -->  
  3. <context:component-scan base-package="com.xx.controller" />  
  4. <context:component-scan base-package="com.xx.scheduler" /> <!-- 用定时器  --></span>  


[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. package com.xx.scheduler;  
  2.   
  3. import java.text.SimpleDateFormat;  
  4. import java.util.Date;  
  5.   
  6. import org.springframework.scheduling.annotation.Scheduled;  
  7. import org.springframework.stereotype.Component;  
  8. @Component  
  9. public class Job {  
  10.     public Job(){    
  11.         System.out.println("MyJob创建成功");    
  12.     }   
  13.     @Scheduled(cron = "0/1 * *  * * ? ")//每隔1秒隔行一次    
  14.     public void run(){    
  15.      System.out.println("Hello MyJob  "+  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ").format(new Date()));    
  16.     }    
  17.   
  18. }  


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值