gpt4 book ai didi

java - 如何将 WebSocketMessageBrokerConfigurer 与应用程序的其余部分提取到单独的包中

转载 作者:行者123 更新时间:2023-12-02 09:27:48 25 4
gpt4 key购买 nike

我正在尝试关注spring.io's websocket tutorial 。只要每个 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 和 de WebSocketMessageBrokerConfigurer-扩展类被注释为@Configuration & @EnableWebSocketMessageBroker

最佳答案

您需要告诉 Spring Boot 在主包之外的何处查找 Spring 组件。您可以使用 @ComponentScan 来实现:

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

@SpringBootApplication 使用 @ComponentScan 注解,但仅对子包有效。

关于java - 如何将 WebSocketMessageBrokerConfigurer 与应用程序的其余部分提取到单独的包中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58214848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com