In the program, some data we want to be ready when the program is started, and only once, in the application scope, we usually use listener to prepare the data. However, using listener to prepare the data for the application scope, there are some minor problems in getting the container.
public class Initlistener implements Servletcontextlistener {///The listener is configured in Web. XML, by default, by reflection generation instance, comes to this object instance to execute/ Does not get from the spring container, Tomcat does not find the spring container, so there is no way to use annotations here//If you use annotations, you generate two objects, a tomcat-generated object, and a spring container-injected object,//tomcat, uses its own generated object. and spring managed objects no one uses public void contextinitialized (Servletcontextevent sce) {//Get globally unique container objects, That is, the container//method configured in Web. Xml that is generated by spring's contextloaderlistener 1:applicationcontext ac = Sce.getservletcontext (). GetAttribute (Webapplicationcontext.root_web_application_context_attribute);//Method 2://wherein Getwebapplicationcontext ( Sce.getservletcontext ()) will automatically find the container object with Root_web_application_context_attribute as key applicationcontext AC = Webapplicationcontextutils.getwebapplicationcontext (Sce.getservletcontext ());//Get the service object through the container Privilegeservice Privilegeservice = (privilegeservice) ac.getbean ("Privilegeserviceimpl");//sce: Current Event//preparation data: Topprivilegelistlist <Privilege> topprivilegelist = Privilegeservice.findtoplist (); Sce.getservletcontext (). SetAttribute (" Topprivilegelist ", topprivilegelist); System.out.println ("------------> Prepared Data topprivilegelist<------------");//Prepare data: allprivilegeurlscollection<string > allprivilegeurls = Privilegeservice.getallprivilegeurls (); Sce.getservletcontext (). SetAttribute (" Allprivilegeurls ", allprivilegeurls); System.out.println ("------------> Prepared Data allprivilegeurls <------------");} public void contextdestroyed (Servletcontextevent arg0) {}