第一步引入接口: ServletContextListener
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
@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文件中配置監聽
1
2
3
|
< listener > < listener-class >com.wbg.treasure.controller.ScheduleController</ listener-class > </ listener > |
以上這篇tomcat啟動完成執行 某個方法 定時任務(Spring)操作就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://www.cnblogs.com/weibanggang/p/11567393.html