Spring Listener Applicationlistener executed 3 times.

Source: Internet
Author: User
<span id="Label3"></p>Premise<p><p>The project uses the spring framework and also uses spring MVC.</p></p>Problem description<p><p>The Applicationlistener interface is then implemented within a class to do something after initialization, but by printing the log, it is discovered that it executes 3 times, one of which is executed when the spring framework is initialized, and two is loaded after the project starts SUCCESSFULLY. Projectname-servlet (that is, Spring MVC).</p></p><p><p>Through the break point see, these two executions, the Onapplicationevent (applicationevent Applicationevent) method in the Applicationevent object value is different, the first is Root Webapplicationcontext</p></p><p><p>The latter two are the project Names. Not sure what the project name was executed two times, guess that spring would scan the entire project multiple times to avoid missing the annotated class. The last time no more loaded into the new class is Stopped. (this paragraph is yy)</p></p>What about the solution?<p><p>I'm Using spring4.<br>Can be handled in such a way, to determine whether the root object initialization, yes, then execute, otherwise do Not.</p></p><pre class="prettyprint"><code class="language-java hljs "><span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">onapplicationevent</span></span>(applicationevent Applicationevent) {<span class="hljs-keyword"><span class="hljs-keyword">Try</span></span>{<span class="hljs-comment"><span class="hljs-comment">//execute only when initializing "root context"</span></span> <span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(applicationevent.getsource ()<span class="hljs-keyword"><span class="hljs-keyword">instanceof</span></span>Xmlwebapplicationcontext) {<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(((xmlwebapplicationcontext) applicationevent.getsource ()). getdisplayname (). equals (<span class="hljs-string"><span class="hljs-string">"Root webapplicationcontext"</span></span>)) {<span class="hljs-comment"><span class="hljs-comment">//... Your code</span></span>} } }<span class="hljs-keyword"><span class="hljs-keyword">Catch</span></span>(Exception E) {log.error (<span class="hljs-string">( <span class="hljs-string">(xmlwebapplicationcontext) applicationevent.getsource ()). getdisplayname () execution failed, Check to see if the spring version supports the "</span></span>); } }</code></pre> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>Spring Listener Applicationlistener executed 3 times.</p></p></span>

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.