Java-加载顺序

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

猜你喜欢

转载自blog.csdn.net/scdncby/article/details/84821540