Preparing application scope data using listener

Source: Internet
Author: User

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 considerations when getting the container.
public class Initlistener implements Servletcontextlistener {public void Contextinitialized (Servletcontextevent SCE) {//Get container with related service object ApplicationContext AC = Webapplicationcontextutils.getwebapplicationcontext (Sce.getservletcontext ()); Privilegeservice Privilegeservice = (privilegeservice) ac.getbean ("Privilegeserviceimpl");// Prepare data:topprivilegelistlist<privilege> topprivilegelist = Privilegeservice.findtoplist (); Sce.getservletcontext (). SetAttribute ("Topprivilegelist", topprivilegelist); System.out.println ("------------> Prepared Data <------------");//Prepare Data:allprivilegeurlscollection<string> Allprivilegeurls = Privilegeservice.getallprivilegeurls (); Sce.getservletcontext (). SetAttribute ("Allprivilegeurls ", Allprivilegeurls); System.out.println ("------------> Prepared Data allprivilegeurls <------------");} public void contextdestroyed (Servletcontextevent arg0) {} 
    1. the listener is configured in Web. XML, by default, by reflection generation instance, to this object instance to execute   
    2. Does not get from the spring container, Tomcat does not find the spring container, so annotations cannot be used here   
    3. If you use annotations, you generate two objects, a tomcat-generated object, a spring container-injected object   
    4. Tomcat uses the objects that it produces, and the objects that spring manages are not used by anyone

Preparing application scope data using listener

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.