我正在嘗試遵循spring.io 的 websocket 教程。只要每個 java 文件都位于同一個包中(我設法進行端到端的 websocket 通信),一切都會正常工作。我嘗試將類提取GreetingController.java到WebSocketConfig.java另一個包中。然后該應用程序將無法再通過 websocket 進行通信。服務器的控制臺表明SimpleBrokerMessageHandler尚未啟動。以下是工作版本和非工作版本的日志:com.example.tutorial.Application : Starting Application on DESKTOP-SOF4KJM with PID 13408com.example.tutorial.Application : No active profile set, falling back to default profiles: defaulto.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 WebApplicationContexto.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1485 mso.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 我怎樣才能讓我的 SpringBoot 應用程序知道另一個包中的這個 websocket 端點,知道 是GreetingController帶注釋的@Controller并且去WebSocketMessageBrokerConfigurer擴展類是帶注釋的@Configuration& @EnableWebSocketMessageBroker?
1 回答

慕哥6287543
TA貢獻1831條經驗 獲得超10個贊
您需要告訴 Spring Boot 在主包之外的何處查找 Spring 組件。您可以使用@ComponentScan它:
@SpringBootApplication
@ComponentScan(value = "com.example.package")
public class Example {...}
@SpringBootApplication注釋為@ComponentScan但僅對子包有效。
添加回答
舉報
0/150
提交
取消