标签:ref 注解 his getbean sbo 其它 bsp 解决方案 bean
方法1:使用静态变量 加 @PostConstruct 解决。
@Component //关键1 public class ArticlesReceiver { @Resource private WechatArticlesTempService wechatArticlesTempService; public static ArticlesReceiver articlesReceiver; //关键2 @PostConstruct //关键3 public void init(){ articlesReceiver = this; } public WechatArticlesTemp getResposeArticlesBoby(String mediaId) { WechatArticlesTemp articlesTemp = articlesReceiver.wechatArticlesTempService.getById(mediaId); //关键4 return articlesTemp ; } }
方法2:使用静态变量,加set注入
@Component //关键1 public class ArticlesReceiver { private static WechatArticlesTempService wechatArticlesTempService; //关键2 @Autowired //关键3 public void setWechatArticlesTempService (WechatArticlesTempService wechatArticlesTempService){ ArticlesReceiver.wechatArticlesTempService = wechatArticlesTempService; } public WechatArticlesTemp getResposeArticlesBoby(String mediaId) { WechatArticlesTemp articlesTemp = wechatArticlesTempService.getById(mediaId); //关键4 return articlesTemp ; } }
方法3:代码注入 , SpringContectHolder类将用到的类的class读入让后再调用类中方法
@Component //关键1 public class ArticlesReceiver { private static WechatArticlesTempService wechatArticlesTempService = SpringContextHolder.getBean(WechatArticlesTempService.class); //关键2 public WechatArticlesTemp getResposeArticlesBoby(String mediaId) { WechatArticlesTemp articlesTemp = wechatArticlesTempService.getById(mediaId); //关键3 return articlesTemp ; } }
参考 https://www.cnblogs.com/04241202-nan/p/11940610.html
在非controller、service里@Resource注入service为null解决方法
标签:ref 注解 his getbean sbo 其它 bsp 解决方案 bean
原文地址:https://www.cnblogs.com/zhuruichao/p/13226505.html