package com.frank.demo@Componentpublic class RegisterService { protected ApplicationContext getContext() { return SpringSingletonUtil.getInstance().getApplicationContext(); } public LoginResult afterRegisterSuccess(int userId, String username, int usernameType, DeviceAdapt deviceAdapt) { // 发布注册成功事件 RegisterSuccessEvent event = new RegisterSuccessEvent(this); event.setUserId(userId); if (usernameType == UsernameUtils.USERNAME_TYPE_MOBILE) { username = "" + UsernameUtils.getMobile(username);// 返回普通手机号 } event.setUsername(username); event.setUsernameType(usernameType); getContext().publishEvent(event); deviceAdapt.setUserId(userId); // 记录注册设备信息 registerInfoHome.recordDeviceInfo(deviceAdapt); return loginService.loginAndRecord(deviceAdapt); }}
RegisterService 代码如上
ApplicationContext的获取package com.frank.demoimport org.springframework.beans.factory.BeanFactoryUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.ApplicationContext;import org.springframework.stereotype.Component;/** * 协助实现spring单例模式,此类主要用于持有ApplicationContext引用 * @author */@Componentpublic class SpringSingletonUtil { private static SpringSingletonUtil instance; @Autowired private ApplicationContext context; private SpringSingletonUtil() { instance = this; } @SuppressWarnings("unchecked") public staticListenerT getBean(Class cls) { return (T)BeanFactoryUtils.beanOfType(instance.context, cls); } public static ApplicationContext getApplicationContext() { return instance.context; } /** * 单例模式 * @return * * @deprecated getBean(Class) */ public static SpringSingletonUtil getInstance() { return instance; }}
package com.frank.demoimport org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.ApplicationEvent;import org.springframework.context.ApplicationListener;import org.springframework.stereotype.Component;/** * *Event* 账户注册成功监听器 **/@Componentpublic class RegisterSuccessListener implements ApplicationListener { private static Logger logger = LoggerFactory.getLogger(RegisterSuccessListener.class); @Override public void onApplicationEvent(ApplicationEvent event) { if (event instanceof RegisterSuccessEvent) { // Do something. } }}
package com.frank.demo;import org.springframework.context.ApplicationEvent;/** * ** 注册成功事件 **/public class RegisterSuccessEvent extends ApplicationEvent { private static final long serialVersionUID = 1L; private int userId; private String username; private int type; /** * 登录名类型:1为手机号,2为邮箱地址 */ public int getType() { return type; } public void setUsernameType(int type) { this.type = type; } public RegisterSuccessEvent(Object source) { super(source); } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } @Override public String toString() { final StringBuilder sb = new StringBuilder("RegisterSuccessEvent{"); sb.append("userId=").append(userId); sb.append(", username='").append(username).append('\''); sb.append(", type=").append(type); sb.append('}'); return sb.toString(); }}
此项目使用了Spring的ApplicationListener的publish和lisener机制。 并且做了一个ApplicationContext的单例操作。 非常巧妙,使用了事件机制来实现应用功能。