十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
第一步引入接口: ServletContextListener
@RestController @RequestMapping("/schedule") public class ScheduleController implements ServletContextListener { @Autowired private ScheduleService scheduleService; @Override public void contextDestroyed(ServletContextEvent event) { System.out.println("销毁"); } @Override public void contextInitialized(ServletContextEvent event) { WebApplicationContextUtils.getRequiredWebApplicationContext(event.getServletContext()) .getAutowireCapableBeanFactory().autowireBean(this); System.out.println("开始执行周期任务"); scheduleService.start(); ; } }