爱程序网

Spring 定时任务配置

来源: 阅读:

1.applicationContext.xml 中 加入task 的声明与xsd

xmlns:task="http://www.springframework.org/schema/task"
http://www.springframework.org/schema/task  http://www.springframework.org/schema/task/spring-task-4.0.xsd

配置中加入

<task:annotation-driven/>

这个是用来启用自动的注解解析。

 

2.编写POJO

@Component 
public class DailyPiracyJob {
	
    Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @Autowired
   	private AppInfoService appInfoService;

	@Scheduled(cron = "0 0 23 * * ?")
	public void scan() throws Exception {
		try {
			List<AppInfo> allAppList = appInfoService.selectAllAppInfo();
			if(null != allAppList && allAppList.size() > 0){
				for(AppInfo appInfo : allAppList){
					appInfoService.insertDailyPiracy(appInfo.getAppMd5());
				}
			}
		} catch (Exception e) {
			logger.error("error when Channel Monitoring.", e);
		}
	}
}

  @Compont 注解,是让Spring context 可以扫描到,并自动注入需要的bean

      @Scheudle 核心注解,不能有返回值,cron是定义了任务运行的间隔,具体,请参考网上其他教程

需要注意的是,在applicationContext.xml中不能启用 default-lazy-init=“true”,否则注解会失效

关于爱程序网 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助