java - 如何将 WebSocketMessageBrokerConfigurer 从应用程序的其余部分提取到单独的包中
问题描述
我正在尝试遵循spring.io 的 websocket 教程。只要每个 java 文件都位于同一个包中,一切都运行良好(我设法进行了端到端的 websocket 通信)。
我试图提取GreetingController.java
和WebSocketConfig.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
?
解决方案
您需要告诉 Spring Boot 在哪里寻找主包之外的 Spring 组件。你可以使用@ComponentScan
它:
@SpringBootApplication
@ComponentScan(value = "com.example.package")
public class Example {...}
@SpringBootApplication
带有注释,@ComponentScan
但仅对子包有效。
推荐阅读
- java - 我有数据库 bur 错误:SQL(查询)错误或缺少数据库
- javascript - JavaScript - 试图根据距离寻找方向
- c# - 如何将 dbcontext/工作单元发送到 Hangfire 作业过滤器
- javascript - 在 npm 中安装后如何要求/导入动漫 js
- r - 与R中的命名“by”连接后如何保留两个键
- ruby-on-rails - 从 Rails 6 beta1 升级到 Rails 6 beta 2 后清除登录和登录重定向中断
- reactjs - ComponentDidUpdate 状态不断变化
- r - 是否可以重定向用户输入以确定第二个用户输入的“选择”(用户输入选项)?
- javascript - 使用符号的视图框不同于使用底层形状的视图框
- angular7 - angular7 查询字符串用户输入未定义的 ID 和密码