WebSocket インスタンス化の注入に失敗しました

 websocket が Bean を注入すると、接続が確立されるたびに次のエラーが報告されます。

java.lang.IllegalArgumentException: Failed to create instance of POJO of type [com.gyk.xyt.chat.server.WebSocketServer]
    at org.apache.tomcat.websocket.pojo.PojoEndpointServer.onOpen(PojoEndpointServer.java:47) ~[tomcat-embed-websocket-9.0.37.jar:9.0.37]
    at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler.init(WsHttpUpgradeHandler.java:133) ~[tomcat-embed-websocket-9.0.37.jar:9.0.37]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:915) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1589) [tomcat-embed-core-9.0.37.jar:9.0.37]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.37.jar:9.0.37]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_311]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_311]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.37.jar:9.0.37]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_311]
Caused by: java.lang.InstantiationException: null
    at org.apache.tomcat.websocket.server.DefaultServerEndpointConfigurator.getEndpointInstance(DefaultServerEndpointConfigurator.java:40) ~[tomcat-embed-websocket-9.0.37.jar:9.0.37]
    at org.apache.tomcat.websocket.pojo.PojoEndpointServer.onOpen(PojoEndpointServer.java:44) ~[tomcat-embed-websocket-9.0.37.jar:9.0.37]
    ... 8 common frames omitted
Caused by: java.lang.NoSuchMethodException: com.gyk.xyt.chat.server.WebSocketServer.<init>()
    at java.lang.Class.getConstructor0(Class.java:3082) ~[na:1.8.0_311]
    at java.lang.Class.getConstructor(Class.java:1825) ~[na:1.8.0_311]
    at org.apache.tomcat.websocket.server.DefaultServerEndpointConfigurator.getEndpointInstance(DefaultServerEndpointConfigurator.java:36) ~[tomcat-embed-websocket-9.0.37.jar:9.0.37]
    ... 9 common frames omitted

configurator = SpringConfigurator.class を @ServerEndpoint アノテーションに追加すると、次のエラーが報告されます。

java.lang.IllegalStateException: Failed to find the root WebApplicationContext. Was ContextLoaderListener not used?
    at org.springframework.web.socket.server.standard.SpringConfigurator.getEndpointInstance(SpringConfigurator.java:70) ~[spring-websocket-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.apache.tomcat.websocket.pojo.PojoEndpointServer.onOpen(PojoEndpointServer.java:44) ~[tomcat-embed-websocket-9.0.37.jar:9.0.37]
    at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler.init(WsHttpUpgradeHandler.java:133) ~[tomcat-embed-websocket-9.0.37.jar:9.0.37]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:915) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1589) [tomcat-embed-core-9.0.37.jar:9.0.37]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.37.jar:9.0.37]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_311]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_311]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.37.jar:9.0.37]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_311]

Baidu でソフト ハンドを検索しても問題を解決できませんでした. この問題は少し奇妙です. サーバーに注入した後も、通常どおり追加、削除、変更、およびチェックを行うことができますが、コンソールには、Bean の注入に失敗したというプロンプトが表示され続けます.

エラーが書き込まれる前に:

/**
 * 作者:轮子哥
 **/

@ServerEndpoint(value = "/ws/WebSocket/{sendId}",configurator = SpringConfigurator.class)
@Service
public class WebSocketServer {

    private final SystemNoticeServer systemNoticeService;
    private final NoticeMonServer noticeMonServer;
    private final InteractionServer interactionServer;

    @Autowired
    public WebSocketServer(SystemNoticeServer systemNoticeService, NoticeMonServer noticeMonServer, InteractionServer interactionServer) {
        this.systemNoticeService = systemNoticeService;
        this.noticeMonServer = noticeMonServer;
        this.interactionServer = interactionServer;
    }

/**
省略
*/

}

絶え間ない試行の後、n時間後に最終的に解決策を見つけました: 

1. @ServerEndpoint アノテーションにconfigurator = SpringConfigurator.class を追加しない

2. Bean インジェクションを静的に変更する

3. コンストラクターを使用しない

例は次のとおりです。

/**
 * 作者:轮子哥
 **/

@ServerEndpoint(value = "/ws/WebSocket/{sendId}")
@Service
public class WebSocketServer {

    private static SystemNoticeServer systemNoticeService;
    private static NoticeMonServer noticeMonServer;
    private static InteractionServer interactionServer;

    @Autowired
    public void setOrderService(SystemNoticeServer systemNoticeService,NoticeMonServer noticeMonServer,InteractionServer interactionServer) {
        WebSocketServer.systemNoticeService = systemNoticeService;
        WebSocketServer.noticeMonServer = noticeMonServer;
        WebSocketServer.interactionServer = interactionServer;
    }


}

setOrderService という名前はランダムに選択されることに注意してください。これは問題ではありませんが、クラス名 WebSocketServer と一致するように変更しないでください。そうしないと、エラーが引き続き報告されます。

おすすめ

転載: blog.csdn.net/qq_46149597/article/details/130059765
おすすめ