ばねspringboot用WebSocket記録処理は、問題を解決する(@Autowired)を注入することができません

WebSocketの@service注釈付けサービスを使用する場合は珍しいチャットメッセージ、送信するときのクラスは、何の問題を起動しない:java.lang.NullPointException、プロセスは、ソリューションの多くを見つけるために、これらの方法が解決されていない、他のがあるでしょういくつかのエラー。
最後に解決され、Citieは後に検査のために記録しました。

第一の実施形態:コンフィギュレータ= SpringConfigurator.classは後で注釈を追加@ServerEndpoint

以下の

@ServerEndpoint(value = "/websocket/{user}", configurator = SpringConfigurator.class)
@Controller

次のエラーはのContextLoaderListenerが実行されていない文字通りの意味から、このように発生します。

java.lang.IllegalStateException: Failed to find the root WebApplicationContext. Was ContextLoaderListener not used?

ContextLoaderListenerは、別の方法には深さがない、いないようだ春ブーツ関係で、春MVC web.xml構成ファイルです。

第二の方法:websockでのconfigure websockクラスコンフィギュレーションクラス

次のとおりです。

@Configuration
public class EndpointConfig {
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        ServerEndpointExporter serverEndpointExporter = new ServerEndpointExporter();
        serverEndpointExporter.setAnnotatedEndpointClasses(WebsocketController.class);
        return serverEndpointExporter;
    }
}

WebsocketControllerのWebSocketの実装クラスは、まだ未解決です。

第三の方法:クラス書き換えApplicationContextAware

次のとおりです。

@Component
public class SpringUtil implements ApplicationContextAware {
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if(SpringUtil .applicationContext == null) {
            SpringUtil .applicationContext = applicationContext;
        }
    }
    public static ApplicationContext getApplicationContext(){
        return applicationContext;
    }

    public static Object getBean(String beanName){
        return applicationContext.getBean(beanName);
    }

    public static <T> T getBean(Class<T> clazz){
        return (T)applicationContext.getBean(clazz);
    }

}

次に、サービスのメソッドを取得するgetBean

private static WeChatService weChatService = SpringUtil .getBean(WeChatService.class);

この方法は非常に実現可能な、ああ、スタート、エラーを思わ...

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'websocketController' defined in file

豆の作成が失敗し、この問題を克服するために準備され、誤って私たちは、別の解決策を見つけました。

4番目の方法:[サービスクラスの初期化、および@Autowiredによって割り当てられ

次のとおりです。

	private static WeChatService weChatService ;

	@Autowired
	public void setWeChatService(WeChatService weChatService){
		this.weChatService = weChatService;
	}

これまでのところ問題は解決されます。

しかし、最終的には第三の方法は実現可能ではないかもしれないが、メッセージは兄である必要があります!

終わり!

公開された24元の記事 ウォン称賛11 ビュー5422

おすすめ

転載: blog.csdn.net/weixin_44037376/article/details/97644830