前言html
我采用的spring框架作的,主要用于IOC AOP ,spring以前采用的2.0版本。(2.0版本出错!下面有解释); 要实现websocket 实现后台主动与JSP发送数据。
具体操做java
在websocket类中 注解添加以下: import org.springframework.web.socket.server.standard.SpringConfigurator; //注解规定了访问的URL @ServerEndpoint(value="/websocket",configurator = SpringConfigurator.class) public class WebSocketServer {} 其中configurator = SpringConfigurator.class 这个表示让spring能扫描到该类,不加的话tomcat会自动启动该websocket类! 加上那句注解以后呢,就能够在websocket类中实现属性注入了。能够引入别的类了,以及使用被的类的方法了。 可是,出现了以下错误! java.lang.NoSuchMethodError: org.springframework.web.context.ContextLoader.getCurrentWebApplicationC 这个错误是什么呢? 可是也能够采用另一种方法引入xml,进行获取要使用的实例: import org.springframework.web.context.ContextLoader; WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext(); ServletContext servletContext = webApplicationContext.getServletContext(); 其中 ContextLoader.getCurrentWebApplicationContext(); 是引入的是spring.jar包中的(core包通常有的把core省略了,包含不少其余的方法,如hibernate方法), 这个方法ContextLoader.getCurrentWebApplicationContext(); 在spring.jar 2.0版本中不存在的,我翻了翻jar包仓库,发现到2.5.6版本sec03中有这个方法了 我是在这个地址看到的spring.jar (2.5.6)包的!http://repo.spring.io/release/org/springframework/spring/2.5.6.SEC03/ 以后就能够把spring2.0包去掉了,换上这个包2.5.6sec03就能够了。 (注意mysql服务要确认开启!别到时候hibernate很差使了。)
关于websocket 采用注解指明URL,采用tomcat启动的方式来作:mysql
除了spring2.5.6.jar包还有以下包: javax.websocket-api-1.1.jar spring-2.5.6.Sec03.jar spring-websocket-4.3.0.RELEASE.jar tomcat7-websocket-7.0.62.jar
总结web
package service; import org.springframework.web.socket.server.standard.SpringConfigurator; import pojo.Goods; import javax.websocket.OnClose; import javax.websocket.OnError; import javax.websocket.CloseReason; import javax.websocket.EndpointConfig; import javax.websocket.OnMessage; import javax.websocket.OnOpen; import javax.websocket.Session; import javax.websocket.server.ServerEndpoint; //注解规定了访问的URL @ServerEndpoint(value="/websocket",configurator = SpringConfigurator.class) public class WebSocketServer { //属性注入 private Goods goods; private GoodsService goodsService; public void setGoods(Goods goods) { this.goods = goods; } public void setGoodsService(GoodsService goodsService) { this.goodsService = goodsService; } public WebSocketServer(){ System.out.println("Socket Constructor!"); } .......... } javax.websocket-api-1.1.jar spring-2.5.6.Sec03.jar spring-websocket-4.3.0.RELEASE.jar tomcat7-websocket-7.0.62.jar
this passage from https://www.cnblogs.com/zhaocundang/p/10057305.html