tomcat启动完成执行 某个方法 定时任务(Spring)

mac2022-06-30  75

第一步引入接口: 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(); ; } }如下图:

 

 

 第二步:在web.xml文件中配置监听

<listener> <listener-class>com.wbg.treasure.controller.ScheduleController</listener-class> </listener>

 

转载于:https://www.cnblogs.com/weibanggang/p/11567393.html

最新回复(0)