zxw394
zxw394
2014-09-01 23:21

spring和websocket的整合,遇到service不能注入到websocket类里面去?

已采纳

package com.openiot.websocket;

import java.io.IOException;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

import org.hibernate.service.spi.InjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import com.openiot.entity.Device;
import com.openiot.entity.ResponseResult;
import com.openiot.service.DeviceService;

//@Component
@ServerEndpoint(value="/websocket")
public class TestWebsocket{

private Session session;
@Autowired
private  DeviceService deviceService;




@OnOpen  
public void open(Session session) {  
    this.session = session;  
    System.out.println("open");
}  

@OnMessage
public void inMessage(String message) {
   try {
    Device device = deviceService.findDeviceById(Long.parseLong(message));
    System.out.println(device);
    session.getBasicRemote().sendText(device.name);
} catch (Exception e) {
    e.printStackTrace();
}
   System.out.println("*** WebSocket Received from sessionId " + this.session.getId() + ": " + message);

}   

@OnClose  
public void end() {  
    System.out.println("*** WebSocket closed from sessionId " + this.session.getId());  
} 

}

debug结果是device为空,在类前面加@Component也试过,也是同样的问题!想请大家分析下

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • ck504323276 ck504323276 7年前

    配置文件呢贴上来啊

    点赞 评论 复制链接分享
  • guixianren123 guixianren123 7年前

    能告诉下怎么解决吗?我也遇到了这个问题

    点赞 1 评论 复制链接分享
  • weixin_42633479 weixin_42633479 7年前

    感觉好吊啊。

    点赞 评论 复制链接分享