spring学习历程 之 事件(二)

   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 BlackListEvent
is 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 BlackListEvent
this is whileListListener of blackListEvent
is fail





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值