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