Spring scheduled task configuration and Spring task Configuration
1. The declaration and xsd for adding tasks to applicationContext. xml
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
Add in configuration
<task:annotation-driven/>
This is used to enable automatic annotation parsing.
2. Compile 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 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.