springboot-@EventListener简单用法


@EventListener简单描述

简化我们编写监听类的步骤,不需要再继承ApplicationListener接口去实现onApplicationEvent了。

 

例子:

@Component
public class TestEventListener {
    @EventListener(classes = {ApplicationEvent.class})
    public void testEvent(ApplicationEvent event){
        System.out.println("done---->"+event.toString());
    }

//     直接在方法里面进行指定也是可以的。不需要再注解里面设置
//    @EventListener()
//    public void testEvent(ApplicationEvent event){
//        System.out.println("done---->"+event.toString());
//    }

    @EventListener()
    public void testEvent2(ApplicationStartedEvent event){
        System.out.println("done--Even2-->"+event.toString());
    }
}

运行结果:

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM