基础支持
先来看看支持哪些默认事件
程序1(service)
先看看程序
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也可以的
/**
* @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接口里面来的
当然了spring想得很周全,不一定通过实现applicationlistener这个类,在bean类里面加入注解@eventlistener
/**
* @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.广州市森锐电子科技有限公司
*/@configurationpublic 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;
}
}