Spring scheduled task configuration and Spring task Configuration
1. The declaration and xsd for adding tasks to applicationContext. xml
1 |
xmlns:task= "http://www.springframework.org/schema/task" |
1 |
http: //www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd |
Add in configuration
1 |
<task:annotation-driven/> |
This is used to enable automatic annotation parsing.
2. Compile POJO
12345678910111213141516171819202122 |
@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 annotation, which enables Spring context to scan and automatically inject the required bean
@ Scheudle core annotation. No return value exists. cron defines the task running interval. For details, refer to other tutorials on the Internet.
Note that default-lazy-init = "true" cannot be enabled in applicationContext. xml; otherwise, the annotation will become invalid.