Spring 事件处理

原创
2015/04/22 22:29
阅读数 54

1、定义事件

public class PurchaseEvent extends ApplicationEvent {

private String message;

private static final long serialVersionUID = 1L;

public PurchaseEvent(Object source, String msg) {
super(source);
this.message = msg;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

}

2、创建事件的发送者

@Service("purchaseService")
public class IPurchaseServiceImpl implements IPurchaseService,ApplicationEventPublisherAware{

@Resource
private PurchaseJPA purchaseJPA;

private ApplicationEventPublisher applicationEventPublisher; 

@Override
public void setApplicationEventPublisher(
ApplicationEventPublisher applicationEventPublisher) {
this.applicationEventPublisher=applicationEventPublisher;
}
}

3、事件的接受着

public class IUserServiceImpl implements IUserService,ApplicationListener{

@Resource
private UserJPA userJPA;

@Resource
private UserMBDAO userMBDAO;

@Override
public void onApplicationEvent(ApplicationEvent event) {
// TODO Auto-generated method stub
if(event instanceof PurchaseEvent){
//MyEvent myEvent = (MyEvent)event; 
User u=new User();
u.setUserName("事件发布成功");
this.createOrUpdateUser(u);
}
}
}

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部