gpt4 book ai didi

java - 在没有 Spring Boot 或 Maven/Gradle 的情况下编写 Spring Websocket 项目

转载 作者:行者123 更新时间:2023-12-05 07:57:58 24 4
gpt4 key购买 nike

如何使用 XML 或 Java Config 但不使用 Spring Boot 创建 Spring Java Web socket 项目。在哪里可以找到分步教程。我不知道如何在 Ecliplse 中使用 Spring Boot。我也不想使用 gradle 或 maven。没有找到在eclipse中使用spring boot的教程。因为我是 Spring 的新手,所以我无法在没有 maven 或 gradle 的情况下开始一个项目。我需要学习如何在没有任何内置工具的情况下创建一个 spring 项目,前提是我需要使用 Eclipse。这纯粹是为了学习目的。

下面是我用来替换Spring boot相关主类的类

AppConfig 类

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@ComponentScan("hello")
@EnableWebMvc
public class AppConfig {



}

WebAppInitializer 类

import javax.servlet.ServletContext;  
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class WebAppInitializer implements WebApplicationInitializer{// extends AbstractAnnotationConfigDispatcherServletInitializer {

public void onStartup(ServletContext servletContext) throws ServletException
{
try
{
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(AppConfig.class);
ctx.setServletContext(servletContext);
Dynamic dynamic = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));

dynamic.addMapping("/");
// dynamic.addMapping("/springStomp/");
dynamic.setLoadOnStartup(1);
//dynamic.setAsyncSupported(true);
//ctx.refresh();
System.out.println("config done");

}
catch(Exception e)
{
e.printStackTrace();
System.out.println("error");
}
}
}

WebSocketConfig 类

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
System.out.println("inside websocket config class");
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/hello").withSockJS();
}
}

剩余与spring web socket tutorial中相同

最佳答案

我知道那种感觉,我的公司网络对我下载依赖项的 Maven 调用不友好。如果您必须像我一样以困难的方式做事,请转到 www.mvnrepository.com 并在搜索中输入 spring,您就可以下载所需的 jar。如果您在部署或编译期间遇到任何 NoClassDef 错误,它通常会告诉您缺少什么,然后在链接中再次搜索关键字。

关于java - 在没有 Spring Boot 或 Maven/Gradle 的情况下编写 Spring Websocket 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25910909/

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