最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot中的ApplicationListener事件監(jiān)聽(tīng)器使用詳解

 更新時(shí)間:2023年11月15日 08:42:27   作者:阿孟呀  
這篇文章主要介紹了SpringBoot中的ApplicationListener事件監(jiān)聽(tīng)器使用詳解,ApplicationListener是應(yīng)用程序的事件監(jiān)聽(tīng)器,繼承自java.util.EventListener標(biāo)準(zhǔn)接口,采用觀察者設(shè)計(jì)模式,需要的朋友可以參考下

ApplicationListener

ApplicationListener是應(yīng)用程序的事件監(jiān)聽(tīng)器,繼承自java.util.EventListener標(biāo)準(zhǔn)接口,采用觀察者設(shè)計(jì)模式。

從spring3.0開(kāi)始,ApplicationListener可以指定要監(jiān)聽(tīng)的事件類(lèi)型,當(dāng)spring的上下文環(huán)境ApplicationContext時(shí),當(dāng)指定事件發(fā)布的時(shí)候,對(duì)應(yīng)事件的事件監(jiān)聽(tīng)器將被調(diào)用。

使用ApplicationListener需要三元素,事件,監(jiān)聽(tīng),事件發(fā)布。

ApplicationListener應(yīng)用

1.新建事件

繼承自ApplicationEvent .

public class TestApplicationEvent extends ApplicationEvent {
    /**
     * Create a new ApplicationEvent.
     *
     * @param test the object on which the event initially occurred (never {@code null})
     */
    public TestApplicationEvent(TestApplication test) {
        super(test);
    }
}

2.新建監(jiān)聽(tīng)器

實(shí)現(xiàn)ApplicationListener指定要監(jiān)聽(tīng)的事件類(lèi)型,監(jiān)聽(tīng)器必須注入容器當(dāng)中可以使用注解方式(例如:@Component),或者在資源路徑下新建META-INF/spring.factories文件導(dǎo)入

@Component
public class TestApplicationListener implements ApplicationListener<TestApplicationEvent> {
 
    @Override
    public void onApplicationEvent(TestApplicationEvent event) {
 
            System.out.println(event.getSource());
    }
}

3.事件發(fā)布

當(dāng)spring應(yīng)用上下文環(huán)境初始化完畢,進(jìn)行事件的發(fā)布

@SpringBootApplication
public class CommonsTestApplication {
 
    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext = SpringApplication
                                                                .run(CommonsTestApplication.class, args);
        TestApplication testApplication = new TestApplication();
        testApplication.setName("zhangsan");
        testApplication.setAge(23);
        TestApplicationEvent testApplicationEvent = new TestApplicationEvent(testApplication);
        applicationContext.publishEvent(testApplicationEvent);//事件發(fā)布時(shí),指定的監(jiān)聽(tīng)器就進(jìn)行接收
    }
}

4.啟動(dòng)程序

當(dāng)容器初始化完畢后,發(fā)布事件,事件監(jiān)聽(tīng)器進(jìn)行接收。

到此這篇關(guān)于SpringBoot中的ApplicationListener事件監(jiān)聽(tīng)器使用詳解的文章就介紹到這了,更多相關(guān)ApplicationListener事件監(jiān)聽(tīng)器使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

鹤峰县| 宁乡县| 丽水市| 辉县市| 海丰县| 两当县| 洛阳市| 保德县| 绥芬河市| 昌平区| 江陵县| 兴和县| 桃江县| 乌苏市| 赣州市| 安庆市| 廉江市| 丰台区| 图木舒克市| 武义县| 泰顺县| 开原市| 三江| 宝山区| 平江县| 布拖县| 洪湖市| 陆川县| 河西区| 洞头县| 玛曲县| 沙坪坝区| 宁陵县| 吐鲁番市| 霍邱县| 柯坪县| 宜阳县| 凌海市| 宣城市| 兖州市| 宁国市|