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>

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM