Spring---ApplicationContext的事件機制

來源:互聯網
上載者:User

標籤:style   blog   io   ar   color   使用   sp   on   div   

事件來源:ApplicationContext。publishEvent()方法:用於主動觸發容器事件。事件:ApplicationEvent類,容器事件,必須由ApplicationContext發布。事件監聽器:ApplicationListener介面,可由容器中任何監聽器Bean擔任。onApplicationEvent(ApplicationEvent event):每當容器內發生任何事件時,此方法都被觸發

容器事件類別需繼承ApplicationEvent類,容器事件的監聽器類需實現ApplicationListener介面。

下面給出一個例子

1.首先定義一個EmailEvent類,繼承ApplicationEvent類

public class EmailEvent extends ApplicationEvent {    private String address;    private String text;    public EmailEvent(Object source) {        super(source);    }    public EmailEvent(Object source, String address, String text) {        super(source);        this.address = address;        this.text = text;    }    public String getAddress() {        return address;    }    public void setAddress(String address) {        this.address = address;    }    public String getText() {        return text;    }    public void setText(String text) {        this.text = text;    }}

2,定義一個EmailNotifier類,實現ApplicationListener介面,並複寫onApplicationEvent()方法

public class EmailNotifier implements ApplicationListener {    @Override    public void onApplicationEvent(ApplicationEvent applicationEvent) {        if (applicationEvent instanceof EmailEvent){            EmailEvent emailEvent = (EmailEvent) applicationEvent;            System.out.println("需要發送郵件的接收地址: " + emailEvent.getAddress());            System.out.println("需要發送郵件的郵件內文: " + emailEvent.getText());        }else {            System.out.println("容器本身的事件: " + applicationEvent);        }    }}

3.將監聽器配置在容器中

<bean class="org.spring.listener.EmailNotifier"/>

4.編寫主程式main

public class SpringTest {    public static void main(String[] args){        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-config.xml");//        EmailEvent ele = new EmailEvent("hello","[email protected]","this is a test");//        ctx.publishEvent(ele);    }}

程式到此結束,運行結果如下:

容器本身的事件: org.springframework.context.event.ContextRefreshedEvent[source=org[email protected]b81eda8: startup date [Thu Dec 04 20:20:52 CST 2014]; root of context hierarchy]

若取消main中的注釋,即,使用publishEvent()來觸發事件,運行結果如下:

容器本身的事件: org.springframework.context.event.ContextRefreshedEvent[source=org[email protected]b81eda8: startup date [Thu Dec 04 20:24:03 CST 2014]; root of context hierarchy]需要發送郵件的接收地址: [email protected]需要發送郵件的郵件內文: this is a test

Spring---ApplicationContext的事件機制

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.