WebSockets JSR 356 Spring 集成@ServerEndpoint [英] WebSockets JSR 356 Spring integration @ServerEndpoint
问题描述
问题:@ServerEndpoint 类中的@Autowired bean 为空
如何确保下面这个WebSocketController类会注入bean,也就是如何让它由Spring管理?我可以连接到 websocket,所以它可以工作,但在 WebSocketController 类实例中,gameService 始终为 null,所以我认为它是由 tomcat 以某种方式创建的,而不是 Spring.
How can I make sure that this WebSocketController class below will be injected with beans, that is how can I make it managed by Spring? I can connect to the websocket so it works but gameService is always null inside the WebSocketController class instance, so I think that it is created by tomcat somehow and not Spring.
我正在使用 Spring Boot.我只需要弄清楚如何将 bean 注入到这个 websocket 控制器类中.
I'm using Spring boot. I just need to figure out how to inject beans into this websocket controller class.
WebSocketController 类
WebSocketController class
@Component
@ServerEndpoint("/sock")
public class WebSocketController {
@Autowired
private GameService gameService;
private static Set<Session> clients = Collections.synchronizedSet(new HashSet<Session>());
@OnMessage
public void handleMessage(Session session, String message) throws IOException {
session.getBasicRemote().sendText(
"Reversed: " + new StringBuilder(message).reverse());
}
@OnOpen
public void onOpen(Session session) {
clients.add(session);
System.out.println("New client @"+session.getId());
if (gameService == null) System.out.println("game service null");
}
@OnClose
public void onClose(Session session) {
clients.remove(session);
System.out.println("Client disconnected @" + session.getId());
}
}
GameService 接口和实现
GameService interface and implementation
public interface GameService {
List<Character> getCharacters();
}
@Service
public class GameServiceMockImpl implements GameService {
@Override
public List<Character> getCharacters() {
List<Character> list = new ArrayList<>();
list.add(new Character("aaa","1.png",100));
list.add(new Character("aaa","2.jpg",100));
list.add(new Character("aaa","3.jpg",100));
return list;
}
}
应用类
@SpringBootApplication
public class App {
public static void main(String args[]){
SpringApplication.run(App.class,args);
}
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
使用 Spring 4 WebSockets 根本不起作用,我什至无法通过浏览器连接.
Using Spring 4 WebSockets doesn't work at all, I can't even connect via a browser.
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(myHandler(), "/myHandler");
}
@Bean
public WebSocketHandler myHandler() {
return new MyHandler();
}
}
public class MyHandler extends TextWebSocketHandler {
@Override
public void handleTextMessage(WebSocketSession session, TextMessage message) {
System.out.println(message.getPayload());
}
}
推荐答案
您正在尝试集成 Spring 和 Java WebSocket API.@Component
注释的类注册到 spring bean,它的实例由 spring 管理,但如果一个类被 @ServerEndpoint
注释,则它注册到服务器端WebSocket 端点,每次相应端点的 WebSocket 连接到服务器时,其实例都由 JWA 实现创建和管理.我们不能同时使用这两个注释.
You are trying to integrate Spring and Java WebSocket API. A class annotated by @Component
is registered to a spring bean and its instance is managed by spring but if a class is annotated by @ServerEndpoint
it is registered to a server-side WebSocket endpoint and every time the corresponding endpoint's WebSocket is connected to the server, its instance is created and managed by JWA implementation. We you can't use both annotations together.
或者你可以使用 CDI 注入(你的服务器也应该支持)
Either you can use CDI injection(your server should also support)
@ServerEndpoint("/sock")
public class WebSocketController {
@Inject
private GameService gameService;
或者看看这个 doc, Spring 4 已经支持 WebSocket
Or have a look on this doc, Spring 4 has support for WebSocket
这篇关于WebSockets JSR 356 Spring 集成@ServerEndpoint的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!