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

第一步引入接口: 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>

 

posted @ 2019-09-22 14:25  韦邦杠  阅读(3184)  评论(0编辑  收藏  举报