Java-載入順序
阿新 • • 發佈:2018-12-05
Java-載入順序
前景
- 公司要求做個重複提交的小程式
- 內網做業務,向公網平臺推送資料
- 斷網不影響業務,網路連線上了,繼續推送,資料不丟失
package com.hfrl.cache; import com.hfrl.dao.CacheMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.*; import java.util.concurrent.TimeUnit; /** * 資料保護 of Dete 2018-12-03 By Chenyb */ @Component public class CacheListMap { //成員變數 private final static String MSG_SUCCESS = "SUCCESS"; private final static String MSG_ERROR = "ERROR"; private final static String MSG_NULL = "NULL"; private static List<Map<String , Object>> cacheList = new ArrayList<>(); private static long timeOut = 500 ; private static boolean config = true ; /*持久層注入*/ @Autowired private static CacheMapper cacheMapper; @Autowired public CacheListMap(CacheMapper cacheMapper){ this.cacheMapper = cacheMapper ; } /*推送資料方法*/ public String pushData ( Map<String , String> dateMap){ System.out.println("cacheList :" + cacheList.size()); if((cacheList == null|| cacheList.size() ==0) && config){ cacheList = initCache(); } if (dateMap != null){ //資料處理 Map map = new HashMap(); String uuid = UUID.randomUUID().toString().replace("-", ""); String cachedata = dateMap.toString().replace("=", ":");//.substring(1, dateMap.toString().length() - 1).replace("=", ":"); map.put("UUID" , uuid); map.put("CACHEDATA",cachedata); cacheList.add(map);//新資料加入快取 Integer addInt = addCache(uuid, cachedata);//新資料持久化 if(addInt < 0 ){ cacheList.remove(map); } System.out.println(cacheList.get(0)); } if(cacheList != null){ try { int countType = 0; StringBuffer strBf = new StringBuffer(); for (Map<String, Object> objectMap : cacheList) { System.out.println(objectMap.get("UUID")); System.out.println(objectMap.get("CACHEDATA")); //推送程式碼 strBf.substring(1,cacheList.size() +1 ); strBf.append("," + objectMap.get("UUID")); countType += 1; } if (countType == cacheList.size()){ //成功,刪除mysql中對應的資料 timeOut = 500 ; Integer delInt = delCache(strBf.substring(1).toString());//資料刪除 if (delInt > 0) { cacheList.clear();//快取清空 countType = 0; } return MSG_SUCCESS; } return MSG_ERROR; }catch (Exception e) { timeOut = timeOut * 2; if(timeOut > 1000*60) timeOut = 1000*60*2;//最大間隔 System.out.println(new Date().toString() + ":" + timeOut); recursionCache(timeOut); return MSG_ERROR; } } return MSG_NULL; } /*初始化*/ private List<Map<String , Object>> initCache (){ config = false ; return cacheMapper.getCache(); } /*新增呼叫*/ private Integer addCache (String uuid , String cachedata){ return this.cacheMapper.addCache(uuid , cachedata); } /*刪除呼叫*/ private Integer delCache (String uuids){ return this.cacheMapper.delCache("(" + uuids + ")"); } /*遞迴呼叫*/ private String recursionCache(long timeOut){ try { TimeUnit.MILLISECONDS.sleep(timeOut);//等待時間 pushData(null); } catch (Exception e1) { new Exception("CacheListMap睡死過去了!"); } return MSG_ERROR; } public static void main(String[] args) { } }
DAO介面注入是個問題,由於載入順序,介面注入物件,一直為空,報錯:初始化異常,空指標等
@Autowired 構造方法方式注入
- 靜態變數或靜態語句塊(按宣告順序)
- 非靜態變數或構造程式碼塊(按宣告順序)
- 構造方法
- @Value/@Autowired等註解
隨筆記錄,方便自己學習
2018-12-05