国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

SpringBoot加載應(yīng)用事件監(jiān)聽器代碼實(shí)例

瀏覽:3日期:2023-05-14 10:58:30

利用 Spring 工廠加載機(jī)制,實(shí)例化 ApplicationListener 實(shí)現(xiàn)類,并排序?qū)ο蠹?/p>

創(chuàng)建應(yīng)用事件監(jiān)聽器

創(chuàng)建類實(shí)現(xiàn)接口ApplicationListener,可以使用@Order或?qū)崿F(xiàn)Orderd接口進(jìn)行排序

@Order(Ordered.HIGHEST_PRECEDENCE)public class HelloWorldApplicationListener implements ApplicationListener<ContextRefreshedEvent> { @Override public void onApplicationEvent(ContextRefreshedEvent event) { System.out.println('HelloWorld : ' + event.getApplicationContext().getId()+ ' , timestamp : ' + event.getTimestamp()); }}

public class AfterHelloWorldApplicationListener implements ApplicationListener<ContextRefreshedEvent>,Ordered { @Override public void onApplicationEvent(ContextRefreshedEvent event) { System.out.println('AfterHelloWorld : ' + event.getApplicationContext().getId()+ ' , timestamp : ' + event.getTimestamp()); } @Override public int getOrder() { return Ordered.LOWEST_PRECEDENCE; }}

在spring.properties中配置

# ApplicationListenerorg.springframework.context.ApplicationListener=com.imooc.diveinspringboot.listener.AfterHelloWorldApplicationListener,com.imooc.diveinspringboot.listener.HelloWorldApplicationListener,

輸出

HelloWorld : application , timestamp : 1591105193644AfterHelloWorld : application , timestamp : 1591105193644

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 高邑县| 芮城县| 祁阳县| 南昌县| 宣汉县| 滕州市| 葫芦岛市| 庆云县| 浦东新区| 靖安县| 青龙| 彭州市| 新邵县| 博罗县| 吉木乃县| 郸城县| 荥经县| 台山市| 读书| 彭泽县| 吴江市| 隆德县| 廊坊市| 宝兴县| 曲阜市| 湖北省| 龙海市| 麦盖提县| 阳信县| 吉水县| 荣成市| 枞阳县| 苏尼特左旗| 西安市| 绥棱县| 布拖县| 锦屏县| 万山特区| 大理市| 容城县| 廉江市|