工具类引用spring依赖为空的解决方式,非spring管理文件调用spring中的注入内容
/**
* @author tong.luo
* @description ParseCompment
* @date 2021/6/10 15:35
*/
@Component
public class ParseCompment {
private static final Logger logger = LoggerFactory.getLogger(ParseCompment.class);
@Autowired
private IGoodsService goodsService;
private static IGoodsService goodsServicestat;
/*@PostConstruct修饰的方法会在服务器加载Servle的时候运行,并且只会被服务器执行一次。PostConstruct在构造函数之后执行,init()方法之前执行*/
@PostConstruct
public void init(){
goodsServicestat = goodsService;
}
/**
*
* @param itemName
* @return
*/
public static Object recommendGoodsCode(String itemName){
Object taxCategories = null;
try {
taxCategories = goodsServicestat.recommendGoodsCode(itemName);
//logger.info("sout[{}]", JSON.toJSONString(taxCategories));
} catch (Exception e) {
logger.error("error",e);
}
return taxCategories;
}
}
工具类文件,非spring管理文件,可以使用上面的方法来避免调用---自动注入出现null的问题

在init方法中进行对象赋值。
本文来自博客园,作者:diligently,转载请注明原文链接:https://www.cnblogs.com/luo12828-foxmail/p/16964009.html

浙公网安备 33010602011771号