The first step in the introduction of interfaces: 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(the this ); System.out.println ( "duty cycle started" ); scheduleService.start ();; } }
as shown below:
Step Two: Configure the Listener in the web.xml file
<listener> <listener-class>com.wbg.treasure.controller.ScheduleController</listener-class> </listener>