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 代碼如上java


ApplicationContext的獲取
spring

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的單例操做。 很是巧妙,使用了事件機制來實現應用功能。
相關文章
相關標籤/搜索