如何在servlet init方法中获取bean对象 5C

public class TuyaListener extends HttpServlet {
/**
*
/
private static final long serialVersionUID = 1L;
private final String url_cn = "pulsar+ssl://mqe.tuyacn.com:7285/";
private final String url_eu = "pulsar+ssl://mqe.tuyaeu.com:7285/";
private final String url_us = "pulsar+ssl://mqe.tuyaus.com:7285/";
private final String accessId = "
************************";
private final String accessKey = "***********************";
@Autowired
private EICBaseDao eicBaseDao;

public void init() throws ServletException{
    super.init();
    /*WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());  
    eicBaseDao = (EICBaseDao)wac.getBean("eICBaseDao");  这种方法不行*/
    startListener();
}

private void startListener(){
    String[] urls = {url_cn,url_eu,url_us};
    for(int i=0;i<urls.length;i++){
        String url = urls[i];
        new Thread(() ->{
             MqConsumer mqConsumer1 = MqConsumer.build().serviceUrl(url).accessId(accessId).accessKey(accessKey)
                     .maxRedeliverCount(3).messageListener(message -> {
                            String jsonMessage = new String(message.getData());
                            MessageVO vo = JSON.parseObject(jsonMessage, MessageVO.class);
                            String str = AESBase64Utils.decrypt(vo.getData(), accessKey.substring(8, 24));
                            JSONObject json = (JSONObject) JSONObject.parse(str);
                            //业务逻辑部分
                            handleTuyaDevice(json);
                         }
                     );
             try {
                mqConsumer1.start();
             } catch (Exception e) {
                e.printStackTrace();
             }
         }).start();
    }
}

private void handleTuyaDevice(JSONObject json){
    String devId = json.getString("devId");
    JSONArray status = json.getJSONArray("status");
    if(null!=status){
        long t0 = System.currentTimeMillis();
        for(int i=0;i<status.size();i++){
            JSONObject item = status.getJSONObject(i);
            String code = item.getString("code");
            long t = item.getLongValue("t");
            if(Math.abs(t0-t)>1000*60){
                return;
            }
            if(item.containsKey("1")){
                boolean value = item.getBooleanValue("1");
                updateDB(devId,code,value);
                break;
            }
            if(item.containsKey("101")){
                boolean value = item.getBooleanValue("101");
                updateDB(devId,code,value);
                break;
            }
        }
    }
}

private void updateDB(String devId,String code,boolean onoff){
    System.out.println("调用数据库");
    String sql = "select devId,devType,info,road from smart_devices where devId='"+devId+"'";
    List<?> list = eicBaseDao.selectAllSql(sql);
    JSONObject json = (JSONObject) list.get(0);
    if(null!=json){
        int temp = onoff?1:0;
        String devType = json.getString("devType");
        StringBuilder sb = new StringBuilder();
        sb.append("update smart_devices ");
        if(devType.equals("switch")){
            String infostr = json.getString("info");
            JSONObject info = JSON.parseObject(infostr);
            Iterator ite = info.keySet().iterator();
            while(ite.hasNext()){
                String key = (String) ite.next();
                Object val = info.get(key);
                if(val instanceof String && code.equals(val) && key.contains("code")){
                    String str = key.replace("code", "");
                    info.put("onoff"+str, onoff);
                    break;
                }
            }
            boolean flag = true;
            Iterator ite2 = info.keySet().iterator();
            while(ite2.hasNext()){
                String key = (String) ite.next();
                if(key.contains("onoff")){
                    int val = info.getIntValue(key);
                    if(val!=temp){
                        flag = false;
                        break;
                    }
                }
            }
            sb.append("set info='");
            sb.append(info.toJSONString());
            sb.append("'");
            if(flag){
                sb.append(" and onoff=");
                sb.append(temp);
            }
        }else{
            sb.append("set onoff=");
            sb.append(temp);
        }
        sb.append(" where devId='");
        sb.append(devId);
        sb.append("'");
        eicBaseDao.executeSql(sb.toString());
    }
}
  1. } ```

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问