ApplicationListener与ApplicationContext的结合使用

package com.frank.demo

@Component
public 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.demo

import 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 
 */
@Component
public class SpringSingletonUtil {

	private static SpringSingletonUtil instance;

	@Autowired
	private ApplicationContext context;

	private SpringSingletonUtil() {
		instance = this;
	}

	@SuppressWarnings("unchecked")
	public static <T> T getBean(Class<T> 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;
	}
}

Listener


package com.frank.demo

import 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;


/**
 * 
 * <pre>
 * 账户注册成功监听器
 * </pre>

 */
@Component
public class RegisterSuccessListener implements ApplicationListener {

	private static Logger logger = LoggerFactory.getLogger(RegisterSuccessListener.class);


	@Override
	public void onApplicationEvent(ApplicationEvent event) {
		if (event instanceof RegisterSuccessEvent) {
			// Do something.

		}
	}
}

Event

package com.frank.demo;

import org.springframework.context.ApplicationEvent;
/**
 * 
 *<pre>
 *	注册成功事件
 *</pre>
 */
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的单例操作。 非常巧妙,使用了事件机制来实现应用功能。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值