ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

利用监听者模式,发布用户变更事件,方便做扩展业务

2022-02-03 22:30:14  阅读:107  来源: 互联网

标签:监听 SqlCommandType 扩展 用户 UserModifyEvent public user event User


定义事件

public class UserModifyEvent extends ApplicationEvent {

	private static final long serialVersionUID = 1L;

	private User user;
	//是新增
	private boolean isInsert;
	//是修改
	private boolean isUpdate;
	//是删除
	private boolean isDelete;

	public UserModifyEvent(User user, SqlCommandType sqlCommandType) {
		super(user);
		this.user = user;
		this.areaId = UserUtils.getLoginAreaId();
		this.isInsert = SqlCommandType.INSERT.equals(sqlCommandType);
		this.isUpdate = SqlCommandType.UPDATE.equals(sqlCommandType);
		this.isDelete = SqlCommandType.DELETE.equals(sqlCommandType);
	}

	public User getUser() {
		return user;
	}

	public boolean isInsert() {
		return isInsert;
	}

	public boolean isUpdate() {
		return isUpdate;
	}

	public boolean isDelete() {
		return isDelete;
	}

}

发布事件

	@Autowired
	ApplicationEventPublisher publisher;

public JsonResult saveUser(User user){
	try{
		userDao.insertSelective(user);
		publisher.publishEvent(new UserModifyEvent(user, SqlCommandType.INSERT));
	catch(DuplicateKeyException e){
		userDao.updateSelective(user);
		publisher.publishEvent(new UserModifyEvent(user, SqlCommandType.UPDATE));
	}catch(Exception e){
		log.error(e);
		throw new LocalException("保存用户失败");
	}
	return OK(user.getId());
}

同步到华为云视频帐号:

public class SameUser2HuaWei implements ApplicationListener<UserModifyEvent> {

	@Override
	public void onApplicationEvent(UserModifyEvent event) {
		User user = event.getUser();
		......
	}

}

给用户发送短信消息提醒

public class SendNoticeWhenCreateUser implements ApplicationListener<UserModifyEvent> {

	@Override
	public void onApplicationEvent(UserModifyEvent event) {
		User user = event.getUser();
		......
	}

}

给用户给用户初始化一些最小权限

public class InitMiniAuthWhenInsertUser implements ApplicationListener<UserModifyEvent> {

	@Override
	public void onApplicationEvent(UserModifyEvent event) {
		User user = event.getUser();
		......
	}
}

给用户关连一些系统必要的设置

public class JoinSettingWhenCreateUser implements ApplicationListener<UserModifyEvent> {

	@Override
	public void onApplicationEvent(UserModifyEvent event) {
		User user = event.getUser();
		......
	}
}

标签:监听,SqlCommandType,扩展,用户,UserModifyEvent,public,user,event,User
来源: https://blog.csdn.net/qq_37148232/article/details/122779441

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有