Springboot:@Component註解下無法@Autowired

這是由於Springboot Bean的加載機制所導致的,具體大家可以去研究源碼。
下面是我的解決方案,僅供參考。

A:

@Component
public class XxxConfig {

	// 1
    public static XxxConfig xxxConfig ;

    @Resource
    private RedisUtil redisUtil;

	// 2
    @PostConstruct
    public void init() {
        xxxConfig = this;
        xxxConfig.redisUtil = this.redisUtil;
    }
    
    public boolean test() {
    	// 3.使用 jwtConfig.redisUtil 形式調用
        return xxxConfig.redisUtil.set("abcd", "123456", 3600);
    }
    
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章