天天看点

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);
    }
    
}