ThreadLocal的使用案例

1.记录用户登陆信息,话不多说(原理自行百度,可参考),直接上代码

public class SessionManager {
    private static ThreadLocal<UserSession> userSession = new ThreadLocal();

    public SessionManager() {
    }

    public static void setUserSession(UserSession userSession) {
        SessionManager.userSession.set(userSession);
    }

    public static UserSession getUserSession() {
        return (UserSession)userSession.get();
    }

    public static ThreadLocal<UserSession> getThreadLocalObject() {
        return userSession;
    }
}
import org.springframework.stereotype.Component;

@Component
public class UserSession {
    private Long userId;
    private String userAccount;
    private String userName;
    private long currentTimeMillis;
    private String role;
    private String roleId;
    private String clientId;
    public UserSession() {
    }

    public Long getUserId() {
        return this.userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public String getUserAccount() {
        return this.userAccount;
    }

    public void setUserAccount(String userAccount) {
        this.userAccount = userAccount;
    }

    public String getUserName() {
        return this.userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }


    public long getCurrentTimeMillis() {
        return this.currentTimeMillis;
    }

    public void setCurrentTimeMillis(long currentTimeMillis) {
        this.currentTimeMillis = currentTimeMillis;
    }

    public String getRole() {
        return this.role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public String getRoleId() {
        return this.roleId;
    }

    public void setRoleId(String roleId) {
        this.roleId = roleId;
    }

    public String getClientId() {
        return this.clientId;
    }

    public void setClientId(String clientId) {
        this.clientId = clientId;
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值