最近在研究Spring 感觉不错。。。。。
ApplicationContext中的事件处理是通过ApplicationEvent
类和ApplicationListener
接口来提供的。如果在上下文中部署一个实现了ApplicationListener
接口的bean,那么每当一个ApplicationEvent
发布到ApplicationContext
时,这个bean就得到通知。
以下是一个关于Spring事件的代码。
package com.xinnuo.spring.event;
/**
* email 对象
* @author 周行
*
*/
public class Email {
private String name;
private String email;
private String subject;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
}
package com.xinnuo.spring.event;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
/**
* 事件监听器
*
* @author 周行
*
*/
public class BlackListNotifier implements ApplicationListener {
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof BlackListEvent) {
Email email = (Email)event.getSource();
System.out.println("name:"+email.getName());
System.out.println("email:"+email.getEmail());
System.out.println("subject:"+email.getSubject());
}
}
}
package com.xinnuo.spring.event;
import org.springframework.context.ApplicationEvent;
/**
* 事件
*
* @author 周行
*
*/
public class BlackListEvent extends ApplicationEvent{
private static final long serialVersionUID = 5866734089078382009L;
public BlackListEvent(Object source) {
super(source);
}
}
package com.xinnuo.spring.event;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
* @author 周行
*
*/
public class EmailBean implements ApplicationContextAware {
private List blackList;
private ApplicationContext ctx;
private Email email;
public void setEmail(Email email) {
this.email = email;
}
public void setBlackList(List blackList) {
this.blackList = blackList;
}
public void setApplicationContext(ApplicationContext ctx) {
this.ctx = ctx;
}
public void sendEmail(String address) {
if (blackList.contains(address)) {
BlackListEvent evt = new BlackListEvent(email);
//发布事件
ctx.publishEvent(evt);
return;
}
}
}
package com.xinnuo.spring.event;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
/**
* Spring 事件测试
* @author 周行
*
*/
public class Demo {
public static void main(String[] args) {
ApplicationContext context = new FileSystemXmlApplicationContext("applicationContext.xml");
EmailBean EmailBean = (EmailBean)context.getBean("emailer");
EmailBean.sendEmail(black@list.org);
}
}
applicationContext.xml
<beans>
<bean id="emailer" class="com.xinnuo.spring.event.EmailBean" abstract="false" singleton="true" lazy-init="default" autowire="default" dependency-check="default">
<property name="blackList">
<list>
<value>black@list.org</value>
<value>white@list.org</value>
<value>john@doe.org</value>
</list>
</property>
<property name="email">
<ref bean="email" />
</property>
</bean>
<bean id="blackListListener" class="com.xinnuo.spring.event.BlackListNotifier"></bean>
<bean id="email" class="com.xinnuo.spring.event.Email" abstract="false" singleton="true" lazy-init="default" autowire="default" dependency-check="default">
<property name="name">
<value>zhouxing</value>
</property>
<property name="email">
<value>zx_zhouxing@sina.com</value>
</property>
<property name="subject">
<value>你好,欢迎光临!</value>
</property>
</bean>
</beans>
jwebee
我的个人网站
posted on 2006-12-01 15:42
周行 阅读(738)
评论(0) 编辑 收藏 所属分类:
IT技术