定时任务类
1.@PropertySource加载配置类-给cron 赋值
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Date;
@PropertySource("classpath:/system.properties")
@Component
public class ServerResourceTask {
@Scheduled(cron = "${Task.notifyTime}")
public void notifyTask() {
System.out.println("定时任务执行:" + new Date());
}
}
system.properties 配置文件内容
Task.notifyTime=0 0/1 * * * ?
2.直接加载配置类赋值
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Date;
public class ServerResourceTask {
@Scheduled(cron = "#{system['Task.notifyTime']}")
public void bandwidthTask() {
System.out.println("定时任务执行:" + new Date());
}
}
system.properties 配置文件内容
Task.notifyTime=0 0/1 * * * ?