使用spring3.1 以注解的形式配置定时任务
当我想把定时任务的时间放入配置文件里去,然后动态获取的时候,总是报
cron expression must consist of 6 fields (found 1 in ${job.schedule})
百思不得其解
通过google在下面这个网址下找到解决办法
https://stackoverflow.com/questions/36418882/scheduled-throwing-exception-for-cron-expression
我的定时任务类:
/**
*
*/
package com.dounion.bas.util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import com.dounion.bas.business.BasSyndataBusiness;
/**
* @author Administrator
*
*/
@Configuration //1.主要用于标记配置类,兼备Component的效果。
@EnableScheduling // 2.开启定时任务
@PropertySource("classpath:/bas.properties")
public class TimerTask_bas{
/** 注入行动Dao类 **/
@Autowired
private BasSyndataBusiness basSyndataBusiness;
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
/**
*
* author: kangyu
* 2018-11-26 下午5:32:29
* @Description: // 查询同步日志表发送给库点(每隔1分钟触发一次)
*/
@Scheduled(cron = "${job.schedule}")
public void sendDataToLibraryPoint() {
basSyndataBusiness.sendDataToLibraryPoint();
}
@Scheduled(cron = "${job.schedule}")
public void dealDataInRedis() {
basSyndataBusiness.dealDataInRedis();
}
}
TimerTask_bas类主要加了以下几行才解决如上bug
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
配置文件bas.properties是这么写的job.schedule = 0 */1 * * * ?
。
application.xml
主要代码:
<context:annotation-config />
<context:component-scan base-package="com.dounion" />
<aop:aspectj-autoproxy proxy-target-class="true" />