springboot中定时执行signForJob()方法,当启动项目加载完配置后执行afterPropertiesSet()方法

代码如下:
package cn.com.casmart.scheduling.job.orders;
import cn.com.casmart.common.domain.common.JResult;
import cn.com.casmart.common.domain.entity.member.Institutes;
import cn.com.casmart.scheduling.api.InstitutesApi;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.List;
import org.springframework.beans.factory.InitializingBean;
@Component
public class OrdersDeliveryJob implements InitializingBean {
private static final Logger logger = LoggerFactory.getLogger(OrdersDeliveryJob.class);
@Autowired
InstitutesApi institutesApi;
@Scheduled(cron = "0 10 2 * * ? ")
public void signForJob() {
try {
JResult<List<Institutes>> jResult = institutesApi.getSelfInspection();
List<Institutes> institutesList = jResult.getData();
} catch (Exception e) {
logger.error("***任务处理时出错", e);
}
}
@Override
public void afterPropertiesSet() throws Exception {
signForJob();
}
}
本文介绍在SpringBoot应用中如何手动运行定时任务,特别是在任务中调用其他接口的情况。通过实现Scheduled接口并重写afterPropertiesSet()方法,确保项目启动后能自动执行signForJob()定时任务。
9729

被折叠的 条评论
为什么被折叠?



