1. 程式人生 > >在工具類中靜態方法使用Spring的元件

在工具類中靜態方法使用Spring的元件

/**
 * @author Dongguabai
 * @date 2018/9/27 10:14
 */
@Component
public class VwOrgSupport {

    /**
     * 可顯示最低級別orgType
     */
    private static final String LOWEST_ORGTYPE_05 = "05";
    private static final String LOWEST_ORGTYPE_06 = "06";

    /**
     * N-表 獲取地區劃分x 軸地區資訊
     * @see VwOrgQuery
     * @param orgId   使用者選擇的區域的orgNo
     * @return  當前柱狀圖x 軸
     */
    public static List<VwOrgQuery> getVwOrgQueryByPOrgNoAndLowOrgType(String orgId){
        String orgType = vwOrgSupport.vwOrgMapper.getOrgTypeByOrgNo(orgId);
        if (LOWEST_ORGTYPE_05.equals(orgType)){
            orgId = orgId.substring(0,orgId.length()-2);
            return vwOrgSupport.vwOrgMapper.getVwOrgQueryByPOrgNoAndLowOrgType(orgType,orgId);
        }
        if (LOWEST_ORGTYPE_06.equals(orgType)){
            orgType = LOWEST_ORGTYPE_05;
            orgId = orgId.substring(0,orgId.length()-4);
            return vwOrgSupport.vwOrgMapper.getVwOrgQueryByPOrgNoAndLowOrgType(orgType,orgId);
        }
        orgType = "0"+(Integer.valueOf(orgType)+1);
        return vwOrgSupport.vwOrgMapper.getVwOrgQueryByPOrgNoAndLowOrgType(orgType,orgId);
    }

    @Autowired
    private VwOrgMapper vwOrgMapper;

    private VwOrgSupport(){}

    private static VwOrgSupport vwOrgSupport;

    @PostConstruct
    public void init() {
        vwOrgSupport = this;
        vwOrgSupport.vwOrgMapper = this.vwOrgMapper;
    }
}

主要是使用了@PostConstruct註解,這個註解會在Servlet容器初始化的時候執行,具體執行時機是在建構函式之後,init()方法之前執行。還有一個@PreDestroy註解,被這個註解修飾的方法在destroy()方法執行執行之後執行。