自己写一个实现ApplicationListener接口并且把该组件加入到容器中
生活随笔
收集整理的這篇文章主要介紹了
自己写一个实现ApplicationListener接口并且把该组件加入到容器中
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
@Component
public class LeonApplicationListener implements ApplicationListener {//接受到消息,回調該方法@Overridepublic void onApplicationEvent(ApplicationEvent event) {System.out.println("LeonApplicationListener 接受到了一個事件"+event);}
}public static void main(String[] args) {AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MainConfig.class);//手動發布一個事件ctx.publishEvent(new ApplicationEvent("我手動發布了一個事件") {@Overridepublic Object getSource() {return super.getSource();}});//容器關閉也發布事件ctx.close();}
測試結果:
LeonApplicationListener 接受到了一個事件org.springframework.context.event.ContextRefreshedEvent[source=org.springframework.context.annotation.AnnotationConfigApplicationContext@37bba400 hierarchy]
LeonApplicationListener 接受到了一個事件com.Leon.testapplicationlistener.MainClass$1[source=我手動發布了一個事件]
LeonApplicationListener 接受到了一個事件org.springframework.context.event.ContextClosedEvent[source=org.springframework.context.annotation.AnnotationConfigApplicationContext@37bba400 hierarchy]
? ??
總結
以上是生活随笔為你收集整理的自己写一个实现ApplicationListener接口并且把该组件加入到容器中的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 通过@Value + @Property
- 下一篇: BeanFactoryPostProce