首页 > 学院 > 开发设计 > 正文

Spring定时任务配置

2019-11-14 15:07:27
字体:
来源:转载
供稿:网友

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”,否则注解会失效


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表