首先是要在消息处理的类中使用注解
@ServerEndpoint("/loginSocket")
public class WebsocketEndPoint extends TextWebSocketHandler {
//这里使用的loginService实在spring配置文件中创建了bean的id
private ILoginService service=(ILoginService) ContextLoader.getCurrentWebApplicationContext().getBean("loginService");
@OnMessage
public void onMessage(String msg,Session session) throws IOException{
System.out.println(service);
System.out.println("客户端传过来的消息"+msg);
SysUser user = service.queryYanZheng(msg);
System.out.println("查询结果:"+user);
if(user!=null){
System.out.println("查到了");
session.getBasicRemote().sendText("Y");
}else{
System.out.println("没查到");
session.getBasicRemote().sendText("N");
}
}
}
因为上面使用getBean的方法调用了spring中的bean,所以在spring.xml配置文件中添加bean
<bean id="loginService"class="com.yuan.system.service.impl.LoginServiceImpl" ></bean>
这样就能解决使用注解的方式不能注入service