【SpingBoot】spring靜態工具類注入問題


package cn.zwqh.action;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Component;

import cn.hkwl.basedata.common.CookieUtil;
import cn.hkwl.basedata.common.constant.ICookieConstants;
import cn.hkwl.basedata.common.constant.ISessionContants;
import cn.hkwl.basedata.common.redis.RedisHandle;
import cn.hkwl.basedata.session.SessionUser;

/**
 * 說明 :Session操作幫助類
 * 
 * @author 朝霧輕寒
 * @version 2018-09-26 09:30
 */

@Component
public class SessionUtils {
    
    @Resource  //(或者@Autowired)
    private RedisHandle redisHandle;

    private static SessionUtils sessionUtils;
    
    @PostConstruct
    public void init() {
        sessionUtils = this;
    }

    /**
     * 
     * 說明 :使用默認request獲取登錄的用戶信息
     * 
     * @throws Exception
     */
    public static SessionUser getLoginUser() throws Exception {
        return (SessionUser) sessionUtils.redisHandle
                .get(CookieUtil.getValue(ServletActionContext.getRequest(), ICookieConstants.LOGIN_USER_KEY));
    }
}

還有點要注意:sessionUtils如果報空指針,需要檢查下當前包名是否被@ComponentScan 掃描到,一開始是總以為是注入有問題,原諒我第一次整合springboot。。。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM