大大大大涛 2017-11-16 02:44 采纳率: 0%
浏览 2697

Websocket通过注解连不上service

用了好的办法就是死活打印不出来service里面的值,使用别的controller可以打印出来service里面的东西
package com.xinhaiwang.controller;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArraySet;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.CloseReason;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

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

import com.xinhaiwang.service.MklService;

@ServerEndpoint("/echo")
public class Websocket {
@Autowired
private MklService mklservice;
/*private MklService mklservice=(MklService) ContextLoader.getCurrentWebApplicationContext().getBean("mklservice");*/
//concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。若要实现服务端与单一客户端通信的话,可以使用Map来存放,其中Key可以为用户标识

private static CopyOnWriteArraySet webSocketSet = new CopyOnWriteArraySet();

//与某个客户端的连接会话,需要通过它来给客户端发送数据

private Session session;

@OnOpen
public void onOpen(Session session) throws IOException {
System.out.println("onOpen");
this.session = session;

webSocketSet.add(this); //加入set中

System.out.println("有新连接加入!");
//以下代码省略...
}
@OnMessage
public String onMessage(String message) throws Exception {
System.out.println("onMessage");
System.out.println("验证连通性:");
/* DmzController dmz = new DmzController();
System.out.println(dmz.mkl());
mklservice.getCount();*/
//向前端传送消息
return "77777";
//以下代码省略...
}
/*@OnMessage(maxMessageSize=6)
public void receiveMessage(String message) {
System.out.println("receiveMessage");
System.out.println("来自客户端的消息:" + message);

//以下代码省略...
}*/
@OnError
public void onError(Throwable t) {
System.err.println("onError");
//以下代码省略...
}

 @OnClose
 public void onClose(Session session, CloseReason reason) {
     System.err.println("onClose");

 //以下代码省略...
 } 
 public void sendMessage(String message) throws IOException{ 
        //调用写好的方法向前端发起请求并一块传值
        this.session.getBasicRemote().sendText(message);  
        //this.session.getAsyncRemote().sendText(message);  
    }

/**
 * 获取数组
 * @throws Exception 
 */

    public String getbean() throws Exception{
        System.out.println("进入到自定义方法");
        //判断时间
         Map<String, Object>map = new HashMap<>();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String now = df.format(new Date());

        String time = "2017-11-19 11:00:00";
        System.out.println("获取当前系统时间"+now);// new Date()为获取当前系统时间
        System.out.println("获取自定义时间"+time);//获取自定义时间
        /*System.out.println("总条数"+service.getCount());*/
        String sjh[] = new String[10];
        map.put("data", sjh);
        //判断是否到抽奖时间
        System.out.println("判断时间:"+!now.equals(time));
        mklservice.getCount();
        System.out.println("获取总条数"+mklservice.getCount());
        /*if(!now.equals(time)){
            //查到中奖的手机号做成数组(加个 判断数组.length是否等于十)
                for (int i = 0; i < 10; i++) {
                    //生成随机数作为id
                    boolean a = true;
                    Integer x=(int)(Math.random()*100);
                    while (a) {
                        System.out.println("随机数:"+x);
                        Integer id = mklservice.getCount();
                        System.out.println("总条数"+id);
                        if(id<x){
                            a = true;
                        }else{
                            a = false;
                            Mkl mkl = mklservice.getbean(x);
                            sjh[i]=mkl.getPhone();
                            mkl.setYuliu1("1");
                        }

                    }
                }
            map.put("data", sjh);
        }*/
            return "";
    }
}
  • 写回答

7条回答

  • 乐古 2017-11-16 03:11
    关注

    可能是你的MklService 没有加注解@Service

    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型