The onapplicationevent in the Applicationlistener interface is called two times solution

Source: Internet
Author: User

After the spring container initialization is complete, call the class Beanpostprocessor, which implements the Applicationlistener interface, overrides the Onapplicationevent method,

The method is the data or cache that we want to load after the container initialization is complete.

Here is the XML configuration:


Here are the Beanpostprocessor classes that you wrote:


Following is the start of Tomcat, repeated loading of data, called two times the Onapplicationevent method:


In the blue section:


Two containers of Root Webapplicationcontext and Webapplicationcontext (Spring-servlet).

The following is the time to initialize the national area information when loaded two times:


Returning JDBC Connection to DataSource has loaded the region information before, and then fetching JDBC Connection from DataSource and loaded again.

Asked the degree Niang: although there are methods, but after the trial still did not solve the problem.

Method One:

Add Judgment in the Onapplicationevent () method,

==null, and then judge the code inside that you want to initialize or cache.

Method Two:


The string value is compared to equals ("Root webapplicationcontext").

Method Three:


Precede the event parameters with final, final, contextrefreshedevent event.

Finally, there is no solution to the problem, perhaps to solve the problem of everyone, write down first.


The onapplicationevent in the Applicationlistener interface is called two times solution

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.