@Component
public class ScheduledUtil {
@PostConstruct
public void initTask(){
System.out.println("任务初始化,此方法就执行一次");
}
@Scheduled(cron = "0/5 * * * * ?")
public void aaa() {
System.out.println("111");
}
}
可以看到服务启动之后,任务没有执行,而是在有接口被请求之后任务类才被加载执行
Compoment在没有被引用时肯定不回创建,你可以
加个public class AppStartListener implements ApplicationListener
或者把这个bean用quarz在xml中配置出来