<div id=\"content_views\" class=\"markdown_views\">
<!-- flowchart 箭头图标 勿删 -->
<svg xmlns=\"http://www.w3.org/2000/svg\" style=\"display: None;\"><path stroke-linecap=\"round\" d=\"M5,0 0,2.5 5,5z\" id=\"raphael-marker-block\" style=\"-webkit-tap-highlight-color: rgba(0, 0, 0, 0);\"></path></svg>
<h3 id=\"0-概述\"><a name=\"t0\"></a>0 概述</h3>
<p>ContextRefreshedEvent 事件会在Spring容器初始化完成会触发该事件。我们在实际工作也可以能会监听该事件去做一些事情,但是有时候使用不当也会带来一些问题。</p>
<h3 id=\"1-防止重复触发\"><a name=\"t1\"></a>1 防止重复触发</h3>
<p>主要因为对于web应用会出现父子容器,这样就会触发两次,那么如何避免呢?下面给出一种简单的解决方案。</p>
<pre class=\"prettyprint\" name=\"code\"><code class=\"hljs axapta has-numbering\">@Component
<span class=\"hljs-keyword\">public</span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class</span> <span class=\"hljs-title\">TestTask</span> <span class=\"hljs-inheritance\"><span class=\"hljs-keyword\">implements</span></span> <span class=\"hljs-title\">ApplicationListener</span><<span class=\"hljs-title\">ContextRefreshedEvent</span>> {</span>
<span class=\"hljs-keyword\">private</span> volatile AtomicBoolean isInit=<span class=\"hljs-keyword\">new</span> AtomicBoolean(<span class=\"hljs-keyword\">false</span>);
@Override
<span class=\"hljs-keyword\">public</span> <span class=\"hljs-keyword\">void</span> onApplicationEvent(ContextRefreshedEvent event) {
<span class=\"hljs-comment\">//防止重复触发</span>
<span class=\"hljs-keyword\">if</span>(!isInit.compareAndSet(<span class=\"hljs-keyword\">false</span>,<span class=\"hljs-keyword\">true</span>)) {
<span class=\"hljs-keyword\">return</span>;
}
start();
}
<span class=\"hljs-keyword\">private</span> <span class=\"hljs-keyword\">void</span> start() {
<span class=\"hljs-comment\">//开启任务</span>
System.out.println(<span class=\"hljs-string\">\"****-------------------init---------------******\"</span>);
}
}</code><ul class=\"pre-numbering\" style=\"\"><li style=\"color: rgb(153, 153, 153);\">1</li><li style=\"color: rgb(153, 153, 153);\">2</li><li style=\"color: rgb(153, 153, 153);\">3</li><li style=\"color: rgb(153, 153, 153);\">4</li><li style=\"color: rgb(153, 153, 153);\">5</li><li style=\"color: rgb(153, 153, 153);\">6</li><li style=\"color: rgb(153, 153, 153);\">7</li><li style=\"color: rgb(153, 153, 153);\">8</li><li style=\"color: rgb(153, 153, 153);\">9</li><li style=\"color: rgb(153, 153, 153);\">10</li><li style=\"color: rgb(153, 153, 153);\">11</li><li style=\"color: rgb(153, 153, 153);\">12</li><li style=\"color: rgb(153, 153, 153);\">13</li><li style=\"color: rgb(153, 153, 153);\">14</li><li style=\"color: rgb(153, 153, 153);\">15</li><li style=\"color: rgb(153, 153, 153);\">16</li><li style=\"color: rgb(153, 153, 153);\">17</li></ul></pre>
<h3 id=\"2-监听事件顺序问题\"><a name=\"t2\"></a>2 监听事件顺序问题</h3>
<p>Spring 提供了一个SmartApplicationListener类,可以支持listener之间的触发顺序,普通的ApplicationListener优先级最低(最后触发)。</p>
<pre class=\"prettyprint\" name=\"code\"><code class=\"hljs java has-numbering\"><span class=\"hljs-annotation\">@Component</span>
<span class=\"hljs-keyword\">public</span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class</span> <span class=\"hljs-title\">LastTask</span> <span class=\"hljs-keyword\">implements</span> <span class=\"hljs-title\">SmartApplicationListener</span> {</span>
<span class=\"hljs-keyword\">private</span> <span class=\"hljs-keyword\">volatile</span> AtomicBoolean isInit = <span class=\"hljs-keyword\">new</span> AtomicBoolean(<span class=\"hljs-keyword\">false</span>);
<span class=\"hljs-annotation\">@Override</span>
<span class=\"hljs-keyword\">public</span> <span class=\"hljs-keyword\">boolean</span> <span class=\"hljs-title\">supportsEventType</span>(Class<? extends ApplicationEvent> eventType) {
<span class=\"hljs-keyword\">return</span> eventType == ContextRefreshedEvent.class;
}
<span class=\"hljs-annotation\">@Override</span>
<span class=\"hljs-keyword\">public</span> <span class=\"hljs-keyword\">boolean</span> <span class=\"hljs-title\">supportsSourceType</span>(Class<?> sourceType) {
<span class=\"hljs-keyword\">return</span> <span class=\"hljs-keyword\">true</span>;
}
<span class=\"hljs-annotation\">@Override</span>
<span class=\"hljs-keyword\">public</span> <span class=\"hljs-keyword\">void</span> <span class=\"hljs-title\">onApplicationEvent</span>(ApplicationEvent event) {
<span class=\"hljs-keyword\">if</span> (!isInit.compareAndSet(<span class=\"hljs-keyword\">false</span>, <span class=\"hljs-keyword\">true</span>)) {
<span class=\"hljs-keyword\">return</span>;
}
start();
}
<span class=\"hljs-keyword\">private</span> <span class=\"hljs-keyword\">void</span> <span class=\"hljs-title\">start</span>() {
<span class=\"hljs-comment\">//开启任务</span>
System.out.println(<span class=\"hljs-string\">\"LastTask-------------------init------------ \"</span>);
}
<span class=\"hljs-comment\">//值越小,就先触发</span>
<span class=\"hljs-annotation\">@Override</span>
<span class=\"hljs-keyword\">public</span> <span class=\"hljs-keyword\">int</span> <span class=\"hljs-title\">getOrder</span>() {
<span class=\"hljs-keyword\">return</span> <span class=\"hljs-number\">2</span>;
}
}</code><ul class=\"pre-numbering\" style=\"\"><li style=\"color: rgb(153, 153, 153);\">1</li><li style=\"color: rgb(153, 153, 153);\">2</li><li style=\"color: rgb(153, 153, 153);\">3</li><li style=\"color: rgb(153, 153, 153);\">4</li><li style=\"color: rgb(153, 153, 153);\">5</li><li style=\"color: rgb(153, 153, 153);\">6</li><li style=\"color: rgb(153, 153, 153);\">7</li><li style=\"color: rgb(153, 153, 153);\">8</li><li style=\"color: rgb(153, 153, 153);\">9</li><li style=\"color: rgb(153, 153, 153);\">10</li><li style=\"color: rgb(153, 153, 153);\">11</li><li style=\"color: rgb(153, 153, 153);\">12</li><li style=\"color: rgb(153, 153, 153);\">13</li><li style=\"color: rgb(153, 153, 153);\">14</li><li style=\"color: rgb(153, 153, 153);\">15</li><li style=\"color: rgb(153, 153, 153);\">16</li><li style=\"color: rgb(153, 153, 153);\">17</li><li style=\"color: rgb(153, 153, 153);\">18</li><li style=\"color: rgb(153, 153, 153);\">19</li><li style=\"color: rgb(153, 153, 153);\">20</li><li style=\"color: rgb(153, 153, 153);\">21</li><li style=\"color: rgb(153, 153, 153);\">22</li><li style=\"color: rgb(153, 153, 153);\">23</li><li style=\"color: rgb(153, 153, 153);\">24</li><li style=\"color: rgb(153, 153, 153);\">25</li><li style=\"color: rgb(153, 153, 153);\">26</li><li style=\"color: rgb(153, 153, 153);\">27</li><li style=\"color: rgb(153, 153, 153);\">28</li><li style=\"color: rgb(153, 153, 153);\">29</li><li style=\"color: rgb(153, 153, 153);\">30</li><li style=\"color: rgb(153, 153, 153);\">31</li><li style=\"color: rgb(153, 153, 153);\">32</li></ul></pre> </div>
版权声明
本文仅代表作者观点,不代表百度立场。
本文系作者授权百度百家发表,未经许可,不得转载。



