In spring MVC, the container is loaded automatically when the project is started, but at ordinary times it needs to be done at the start of the project, which is used in the container, and can be accomplished by implementing the Applicationlistener interface.
A class that implements the Applicationlistener interface that is notified whenever a applicationevent is published to the ApplicationContext (that is, the context, also known as a container), Next, load a timed task at the start of the project as an example
Package com. Timer
Import Java.util.Date;
Import java.util.List;
Import Java.util.Timer;
Import Java.util.TimerTask;
Import javax.servlet.ServletContextEvent;
Import Javax.servlet.ServletContextListener;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.context.ApplicationListener;
Import org.springframework.context.event.ContextRefreshedEvent;
Import Org.springframework.stereotype.Service;
PS implementation annotation, for the container to scan the class
@Service
public class Tasktimer implements applicationlistener<contextrefreshedevent>{
@Autowired
Contents of containers used by private Xxxservice xxxservice;//
Private timer timer = null;
@Override
public void Onapplicationevent (Contextrefreshedevent evt) {
if (Evt.getapplicationcontext (). GetParent () = null) {
Createsitemap ()//calling the task to do
}
}
private void Createsitemap () {
Timer timer = new Timer ("Createsitemap", true);
Timer.schedule (New TimerTask () {
@Override
public void Run () {
Date Sysdatestar = new Date ();
System.out.println (Sysdatestar);
Date sysdateend = Dateutil.adddateminut (Sysdatestar, 10);
What you want to do when you start a project
}
}, 5000, 10*60000);
}
}