浩晨众云网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
第一步引入接口: 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(); ; } }