Spring 註解事件Event

String event

從Spring的4.2版本後,開始支持註解來進行事件廣播接收,這使得咱們很是方便 固然了Spring也支持JMS消息中間件,這個就能夠作多個系統集成了,感受有點偏題了,先看看事件怎麼經過註解來開發java


基礎支持

先來看看支持哪些默認事件spring

Event 描述
ContextRefreshedEvent ApplicationContext或者叫spring被初始化或者刷新initialized會觸發該事件
ContextStartedEvent spring初始化完,時觸發
ContextStoppedEvent spring中止後觸發,一箇中止了的動做,能夠經過start()方法重新啓動
ContextClosedEvent spring關閉,全部bean都被destroyed掉了,這個時候不能被刷新,或者重新啓動了
RequestHandledEvent 請求通過DispatcherServlet時被觸發,在request完成以後

程序1(Service)

先看看程序app

ApplicationEventPublisher這個是spring的東西,須要注入來進行發送 由於實現了ApplicationEventPublisherAware因此setApplicationEventPublisher這個方法會自動幫咱們調用,拿到廣播發送者學習

/**
 * @author Carl
 * @date 2016/8/28
 * @modify 版權全部.(c)2008-2016.廣州市森銳電子科技有限公司
 */
public class EmailService implements ApplicationEventPublisherAware {

    private List<String> blackList;
    private ApplicationEventPublisher publisher;

    public void setBlackList(List<String> blackList) {
        this.blackList = blackList;
    }

    public void setApplicationEventPublisher(ApplicationEventPublisher publisher) {
        this.publisher = publisher;
    }

    /**
     * 具體廣播類
     * @param address
     * @param text
     */
    public void sendEmail(String address, String text) {
        if (blackList.contains(address)) {
            BlackListEvent event = new BlackListEvent(this, address, text);
            publisher.publishEvent(event);
            return;
        }
        // send email...
    }
}

程序2(Event)

這裏也是須要繼承ApplicationEvent,而且裏面能夠實現本身的一些必要參數等等,讓在收到廣播時進行獲取,固然經過source也能夠的this

/**
 * @author Carl
 * @date 2016/8/28
 * @modify 版權全部.(c)2008-2016.廣州市森銳電子科技有限公司
 */
public class BlackListEvent extends ApplicationEvent {
    private String address;
    private String test;

    public BlackListEvent(Object source, String address, String test) {
        super(source);
        this.address = address;
        this.test = test;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getTest() {
        return test;
    }

    public void setTest(String test) {
        this.test = test;
    }
}

程序3(receiver)

用spring仍是得遵循他一套規範,那麼接收者的,還得實現ApplicationListener接口,那麼全部收到泛型廣播的對象,都會轉發onApplicationEvent接口裏面來的spa

固然了spring想得很周全,不必定經過實現ApplicationListener這個類,在bean類裏面加入註解@EventListenercode

/**
 * @author Carl
 * @date 2016/8/28
 * @modify 版權全部.(c)2008-2016.廣州市森銳電子科技有限公司
 */
public class BlackListNotifier implements ApplicationListener<BlackListEvent> {

    private String notificationAddress;

    public void setNotificationAddress(String notificationAddress) {
        this.notificationAddress = notificationAddress;
    }

    @EventListener
    public void onApplicationEvent(BlackListEvent event) {
        // notify appropriate parties via notificationAddress...
        System.out.println("onApplicationEvent, some thing I receive:" + event.getAddress() + ",text:" + event.getTest());
    }

    @EventListener(condition = "#event.test == 'foo'")
    public void onApplicationCustomerEvent(BlackListEvent event) {
        System.out.println("onApplicationCustomerEvent,some thing I receive:" + event.getAddress() + ",text:" + event.getTest());
        // notify appropriate parties via notificationAddress...
    }

    @EventListener({ContextStartedEvent.class, ContextRefreshedEvent.class})
    public void handleContextStart() {
        System.out.println("-------------handleContextStart");

    }
    
    /**
     * 參數能夠給BlackListEvent 能夠不給
     */
    @EventListener(classes = {BlackListEvent.class})
    public void handleBlackListEvent() {
        System.out.println("-------------handleBlackListEvent");
    }
}

@EventListener

解析一下這個註解怎麼用,猶如上面的程序,除了實現接口外,能夠經過@EventListener註解來實現中間件

  • condition可使用SpEL表達式,就是當知足條件才執行
  • classes當觸發event對象是這個class纔會被執行

程序4(config bean)

這裏主要對一些服務以及接受廣播bean的註冊,以便接受對象

/**
 * 配置
 * @author Carl
 * @date 2016/8/28
 * @modify 版權全部.(c)2008-2016.廣州市森銳電子科技有限公司
 */
@Configuration
public class AppConfig {
    @Bean
    public EmailService emailService() {
        EmailService s = new EmailService();
        List<String> emails = new ArrayList<>(3);
        emails.add("known.spammer@example.org");
        emails.add("known.hacker@example.org");
        emails.add("john.doe@example.org");
        s.setBlackList(emails);
        return s;
    }

    @Bean
    public BlackListNotifier notifier() {
        BlackListNotifier notifier = new BlackListNotifier();
        notifier.setNotificationAddress("blacklist@example.org");
        return notifier;
    }
}

我的學習記錄說得不對麻煩你們諒解,或進行評論補充繼承

相關文章
相關標籤/搜索