使用spring框架后在静态类中使用注入对象报错

1:使用spring boot微服务时,想创建一个静态util类供其他地方调用

2:使用@PostConstruct注解初始化注入对象,此注解在:当前类中只能使用一次,而且不能是static,只能返回void,不能允许有参数。

3:此方法执行是在依赖注入完成后执行。可使用静态变量接收初始化的接口。

4:完整代码如下:

package com.x3.bills.factory.utils;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.x3.b2b.sdk.entity.BillPrice;
import com.x3.b2b.sdk.entity.ParamBillPrice;
import com.x3.b2b.sdk.service.IB2BService;
import com.x3.base.core.exception.BusinessException;
import com.x3.base.core.util.DoubleUtil;
import com.x3.base.core.util.StringUtil;
import com.x3.bills.base.archives.biz.IclothingBiz;
import com.x3.bills.base.archives.biz.IparamBiz;
import com.x3.bills.base.archives.entity.ClothingInfoEntity;

@Component
public class SuppPriceUtil {
	
	private static IparamBiz curParamBiz;
	
	private static IB2BService curB2bService;
	
	private static IclothingBiz curClothingBiz;
	
	@Autowired
	private IparamBiz paramBiz;
	
	@Autowired
	private IB2BService b2bService;
	
	@Autowired
	private IclothingBiz clothingBiz;
	
	@PostConstruct
	private void Init(){
		curParamBiz=this.paramBiz;
		curB2bService=this.b2bService;
		curClothingBiz=this.clothingBiz;
	}
	
	/**
	 * 根据货品、及收发地等信息获取供应商结算价格 和系统参数1051有关
	 * @param clos 货品集合
	 * @param set_depot_id 发货地ID
	 * @param get_depot_id 收货地ID
	 * @param sell_class_id 发货类型,可为空
	 * @return
	 */
	public static List<BillPrice> getSuppPrice(List<String> clos,String set_depot_id,String get_depot_id,String sell_class_id){
		//验证参数
		if(clos==null || clos.size()==0){
			throw new BusinessException("SuppPriceUtil-getPrice-01", "获取供应商价格失败,参数clos至少包含一个元素!");
		}
		if(StringUtil.isBlankOrNull(set_depot_id)){
			throw new BusinessException("SuppPriceUtil-getPrice-02", "获取供应商价格失败,参数set_depot_id不允许为空!");
		}
		if(StringUtil.isBlankOrNull(get_depot_id)){
			throw new BusinessException("SuppPriceUtil-getPrice-03", "获取供应商价格失败,参数get_depot_id不允许为空!");
		}
		
		String p_1051=curParamBiz.getParameterValue(1051, "");
		if(StringUtil.isBlankOrNull(p_1051)){
			throw new BusinessException("SuppPriceUtil-getPrice-04", "获取供应商价格失败,请联系系统管理员检查系统参数1051是否设置正确!");
		}
		
		switch(p_1051){
		case "0"://实际成本
		case "1"://采购成本
		case "2"://吊牌价
			//折扣保留6位小数
			return getOtherPrice(clos,p_1051);
		case "3":
			return getPolicyPrice(clos,set_depot_id,get_depot_id,sell_class_id);
		default:
			throw new BusinessException("SuppPriceUtil-getPrice-05", "获取供应商价格失败,非法类型!");
		}
	}
	
	
	/**
	 * 获取实际成本,采购成本,吊牌价
	 * @param clos clothing_id 集合
	 * @param p_1051 参数0,1,2
	 * @return
	 */
	private static List<BillPrice> getOtherPrice(List<String> clos,String p_1051){
		List<ClothingInfoEntity> closlist=curClothingBiz.getClothingInfo(clos);
		List<BillPrice> list=new ArrayList<>();
		if(list!=null && list.size()>0){
			BillPrice priceEntity=null;
			for(ClothingInfoEntity closEntity:closlist){
				priceEntity=new BillPrice();
				priceEntity.setClothing_id(closEntity.getClothing_id());
				switch(p_1051){
					case "0"://实际成本
						priceEntity.setS_price(closEntity.getCost());
						//折扣=实际成本价/吊牌价
						priceEntity.setSale_rate(DoubleUtil.round(DoubleUtil.div(closEntity.getCost(), closEntity.getClothing_jprice()),6));
						break;
					case "1"://采购成本
						priceEntity.setS_price(closEntity.getCost1());
						//折扣=采购成本价/吊牌价
						priceEntity.setSale_rate(DoubleUtil.round(DoubleUtil.div(closEntity.getCost1(), closEntity.getClothing_jprice()),6));
						break;
					case "2"://吊牌价 1折
						priceEntity.setS_price(closEntity.getClothing_jprice());
						priceEntity.setSale_rate(1);
						break;
					default:
						throw new BusinessException("SuppPriceUtil-getOtherPrice-01", "获取供应商价格失败,非法类型!");
				}
				priceEntity.setJ_price(closEntity.getClothing_jprice());
				list.add(priceEntity);
			}
		}
		return list;
	}
	
	/**
	 * B2B 获取供应商政策价格
	 * @param clos clothing_id 集合
	 * @param set_depot_id 发货地ID
	 * @param get_depot_id 收货地ID
	 * @param sell_class_id 发货类型
	 * @return
	 */
	private static List<BillPrice> getPolicyPrice(List<String> clos,String set_depot_id,String get_depot_id,String sell_class_id){
		ParamBillPrice price_param = new ParamBillPrice();
        price_param.setClothing_id(clos);
        price_param.setFlow_type(5);
        price_param.setSet_depot_id(set_depot_id);
        price_param.setGet_depot_id(get_depot_id);
        price_param.setSet_selltype_id(sell_class_id);
        if (curB2bService == null) {
            throw new BusinessException("SuppPriceUtil-getPrice-04", "获取供应商价格失败,B2B SDK实例为空!");
        }
        return curB2bService.getBillPrice(price_param);
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章