【发布时间】:2014-09-18 11:02:03
【问题描述】:
如何创建 Spring Java Web 套接字 项目使用 XML 或 Java 配置,但没有 Spring Boot。在哪里可以找到分步教程。我不知道如何在 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();
}
}
剩余与spring web socket tutorial中的相同
【问题讨论】:
-
相信我,你想使用 maven 或 gradle。您不想花费数小时在 Internet 上搜索所有需要且正确的依赖项。它将为您节省大量时间。对于初学者,请查看弹簧参考指南。
-
我在一个学校封闭的网络中。所以在这里我无法使用 maven,因为它正在下载并且我们的网络防火墙阻止了它。
-
那么,如果防火墙禁止这样做,您希望如何自己下载所需的 jars/zip?如果 maven 无法下载,则无法下载。
-
这里管理员将下载我们需要的所有 jars。所以到目前为止,我几乎拥有Spring web socket tutorial 所需的所有罐子。现在我被这个弹簧靴困住了。你能告诉我如何在没有弹簧启动的情况下编码。
-
@SathishKumarkk 虽然短期内可能不可行,但您需要让管理员为学校设置本地存储库。查看sonatype.org/nexus 和jfrog.com/open-source/#os-arti
标签: java eclipse spring maven spring-websocket