spring学习历程 之 事件(二)

来源:互联网 发布:网络棋牌判刑 编辑:程序博客网 时间:2024/05/18 01:30

   ApplicationContext 中的事件处理是通过ApplicationEvent类和ApplicationListener接口来提供的。如果在上下文中部署一个实现了ApplicationListener接口的bean,

那么每当一个ApplicationEvent发布到ApplicationContext时,这个bean就得到通知。实质上,这是标准的Observer设计模式。

   只要在ApplicationContext调用publishEvent()方法可以很方便的实现自定义事件,将一个实现了ApplicationEvent的自定义事件类作为参数就可以了。事件的监听器同步

的接收事件。这意味着publishEvent()方法将被阻塞。直到所有的监听器都处理完事件(可以通过一个ApplicationEventMulticaster的实现提供可选的事件发送策略)。此外,

如果事务context可用,监听器会接收一含有发送者事务context的事件。


   下面一个例子:

事件的定义:

public class BlackListEvent extends ApplicationEvent{private String address; private String text;public BlackListEvent(){super(new Object());}public BlackListEvent(String address,String text){super(address);this.address = address;this.text = text;}public String getAddress(){return address;}public String getText(){return text;}}
实际的类:

public class EmailBean implements ApplicationContextAware {    private List blackList;private ApplicationContext ctx;    public void setBlackList(List blackList) {        this.blackList = blackList;    }    public void setApplicationContext(ApplicationContext ctx) {        this.ctx = ctx;    }    public void sendEmail(String address, String text) {        if (blackList.contains(address)) {            BlackListEvent event = new BlackListEvent(address, text);            ctx.publishEvent(event);
            <span style="color:#ff0000;">System.out.println("is fail");</span>            return;        }       <span style="color:#ff0000;"> System.out.println("is seccess");</span>    }}
监听器的定义:

public class BlackListNotifier implements ApplicationListener {    private String notificationAddress;        public void setNotificationAddress(String notificationAddress) {        this.notificationAddress = notificationAddress;    }    public void onApplicationEvent(ApplicationEvent event) {        if (event instanceof BlackListEvent) {            <span style="color:#ff0000;">System.out.println("this event is BlackListEvent");.</span>        }    }}
bean的配置:

<pre name="code" class="html"><beans><bean id="emailer" class="cn.cong.spring.bean.test.SpringEmailBean"><property name="blackList">    <list>        <value>black@list.org</value>        <value>white@list.org</value>        <value>john@doe.org</value>    </list></property> </bean><bean  id="blackListListener" class="cn.cong.spring.bean.test.BlackListNotifier">   <property name="notificationAddress" value="spam@list.org" /></bean> </beans>


测试类:

public class SpringMain {public static void main(String[] arge){ApplicationContext context  = new ClassPathXmlApplicationContext(new String[]{"spring.xml"});SpringEmailBean seb = (SpringEmailBean)context.getBean("emailer");seb.sendEmail("black@list.org",null);}}

结果:

this event is BlackListEventis fail

publishEvent()方法加入事件之后,它会去查找所有的监听器的,现在再一个监听器:

public class WhileListListener implements ApplicationListener{public void onApplicationEvent(ApplicationEvent event){if(event instanceof BlackListEvent){System.out.println("this is whileListListener of blackListEvent");}}}

在bean配置

<beans><bean id="emailer" class="cn.cong.spring.bean.test.SpringEmailBean"><property name="blackList">    <list>        <value>black@list.org</value>        <value>white@list.org</value>        <value>john@doe.org</value>    </list></property> </bean><bean  id="blackListListener" class="cn.cong.spring.bean.test.BlackListNotifier">   <property name="notificationAddress" value="spam@list.org" /></bean> <span style="color:#ff0000;"><bean id="whileListListener" class="cn.cong.spring.bean.test.WhileListListener"/></span></beans>
运行测试类结果为:

this event is BlackListEventthis is whileListListener of blackListEventis fail





0 0
原创粉丝点击