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