昨天在写一个util类时遇到注入service层失败,想了下普通类注入bean,直接使用@Component+@Autowired是注入不了的,回顾一下,解决步骤如下: 1、定义==静态对象== 2、定义==初始化方法==用@PostConstruct将需要对象引入

案例demo如下:

@Component
public class StrategyUtil {
    private static StrategyUtil strategyUtil;
    //货位业务
    @Autowired
    private  IStraGoodsPositionService straGoodsPositionService;
    //上架记录业务
    @Autowired
    private  IStraGroundingRecordService straGroundingRecordService;
    //货架业务
    @Autowired
    private  IStraSheifService straSheifService;

    @PostConstruct
    public void init(){
        strategyUtil=this;
        strategyUtil.straGoodsPositionService = this.straGoodsPositionService;
        strategyUtil.straGroundingRecordService = this.straGroundingRecordService;
        strategyUtil.straSheifService = this.straSheifService;
    }
}


//使用时strategyUtil.straGoodsPositionService.方法;
//例如:
strategyUtil.straGoodsPositionService.GetCount(warehouse_id);