首页 > 解决方案 > 如何将 WebSocketMessageBrokerConfigurer 从应用程序的其余部分提取到单独的包中

问题描述

我正在尝试遵循spring.io 的 websocket 教程。只要每个 java 文件都位于同一个包中,一切都运行良好(我设法进行了端到端的 websocket 通信)。

我试图提取GreetingController.javaWebSocketConfig.java类到另一个包中。该应用程序将无法再通过 websocket 进行通信。

服务器的控制台提示SimpleBrokerMessageHandler未启动。以下是工作和非工作版本的日志:

com.example.tutorial.Application         : Starting Application on DESKTOP-SOF4KJM with PID 13408
com.example.tutorial.Application         : No active profile set, falling back to default profiles: default
o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
o.apache.catalina.core.StandardService   : Starting service [Tomcat]
org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.24]
o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1485 ms
o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'clientInboundChannelExecutor'
o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'clientOutboundChannelExecutor'
o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService 'messageBrokerTaskScheduler'
o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'brokerChannelExecutor'
o.s.b.a.w.s.WelcomePageHandlerMapping    : Adding welcome page: class path resource [static/index.html]
o.s.m.s.b.SimpleBrokerMessageHandler     : Starting...
o.s.m.s.b.SimpleBrokerMessageHandler     : BrokerAvailabilityEvent[available=true, SimpleBrokerMessageHandler [DefaultSubscriptionRegistry[cache[0 destination(s)], registry[0 sessions]]]]
o.s.m.s.b.SimpleBrokerMessageHandler     : Started.
o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
com.example.tutorial.Application         : Started Application in 2.631 seconds (JVM running for 3.04)

com.example.tutorial.Application         : Starting Application on DESKTOP-SOF4KJM with PID 2508
com.example.tutorial.Application         : No active profile set, falling back to default profiles: default
o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
o.apache.catalina.core.StandardService   : Starting service [Tomcat]
org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.24]
o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1509 ms
o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
o.s.b.a.w.s.WelcomePageHandlerMapping    : Adding welcome page: class path resource [static/index.html]
o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
com.example.tutorial.Application         : Started Application in 2.52 seconds (JVM running for 2.873)
o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
o.s.web.servlet.DispatcherServlet        : Completed initialization in 5 ms

我怎样才能让我的 SpringBoot 应用程序知道另一个包中的这个 websocket 端点,知道GreetingController被注释@Controller和去WebSocketMessageBrokerConfigurer扩展类被注释@Configuration& @EnableWebSocketMessageBroker

标签: javaspringspring-bootwebsocket

解决方案


您需要告诉 Spring Boot 在哪里寻找主包之外的 Spring 组件。你可以使用@ComponentScan它:

@SpringBootApplication
@ComponentScan(value = "com.example.package")
public class Example {...}

@SpringBootApplication带有注释,@ComponentScan但仅对子包有效。


推荐阅读