tomcat start timing of completion method to perform a task (Spring)

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>

 

Guess you like

Origin www.cnblogs.com/weibanggang/p/11567393.html