ServerEndpoint 无法注入bean的问题


在springmvc项目中配置websocket的环境,给客户端发送消息模块,变量无法注入,值为空

 

  @Autowired
  private Mydata mydata;

/**
     * 收到客户端消息后调用的方法
     * @param message 客户端发送过来的消息
     * @param session 可选的参数
     * @throws EncodeException 
     */
    @OnMessage
    public void onMessage(String message, Session session) {
        System.out.println("来自客户端的消息:" + message);
        //群发消息
        for(JdeviceController item: webSocketSet){
            try {
                int yy = mydata.findAll().size();
                String teee = "webtest" + yy;
                item.sendMessage(teee);
            } catch (IOException e) {
                e.printStackTrace();
                continue;
            }
        }
    }

mydata值为空导致websocket连接关闭。原来代码如下

@Controller
@ServerEndpoint( "/websocket")
public class MydataController extends BaseController {
}

通过设置ServerEndpoint(value = "/websocket",configurator = SpringConfigurator.class)解决问题,代码如下

@Controller
@ServerEndpoint(value = "/websocket",configurator = SpringConfigurator.class)
public class MydataController extends BaseController {
}

需要添加的jar为:spring-websocket-4.3.8.RELEASE

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM