如何使用 XML 或 Java Config但没有Spring Boot创建Spring Java Web 套接字项目。在哪里可以找到分步教程。我不知道如何在 ecliplse 中使用 spring boot。我也不想使用 gradle 或 maven。我没有找到在eclipse中使用spring boot的教程。由于我是 spring 新手,我无法在没有 maven 或 gradle 的情况下启动项目。如果我需要使用 Eclipse,我需要学习如何在没有任何内置工具的情况下创建一个 spring 项目。这纯粹是为了学习目的。
下面是我用来替换 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();
}
}