springboot中@Autowired注入为null

问题描述

在项目中,对一些工具类使用了@Component标注,然后在service层去注入该工具类,断点显示注入的工具类为null。

解决方案

@Component
@Slf4j
public class MailUtils {

    public static MailUtils mailUtils;

    @PostConstruct
    public void init(){
        mailUtils = this;
    }
}

调用时使用MailUtils.mailUtils.methodName()进行调用

posted @ 2022-01-11 11:31  Cedrus  阅读(108)  评论(0)    收藏  举报