简体   繁体   中英

Write a Spring Web Socket project without SpringBoot or Maven/Gradle

How to create a Spring Java Web socket project with XML or Java Config but without Spring Boot . Where can I find a step-by-step tutorial. I dont know how to use spring boot in ecliplse. Also I dont want to use gradle or maven. I did not find a tutorial to use spring boot in eclipse. As I am new to spring I am unable to start a project without maven or gradle. I need to learn how to create a spring project without any built tool provided I need to use Eclipse. This is purly for learning puropse.

Below is the classes I used to replace Spring boot related main class

AppConfig Class

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 Class

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 Class

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();
    }
}

Remaining is same as in spring web socket tutorial

I know how that feels, my company network is unfriendly towards my maven calls to download dependencies. If you had to do things the hard way like me goto www.mvnrepository.com and just type spring in the search and you can download the jars you need. If you encounter any NoClassDef errors during deployment or compilation it usually tells you what you are missing, then search for the keywords again in the link.

Just:

  1. add gradle plugin in eclipse
  2. import following project from spring site as gradle project
  3. ???
  4. Profit!

Than if you don't want use spring boot remove any spring boot dependincies, add regular deps like spring-context, spring-webmvc, etc. And finally add spring-websocket and spring-messaging libraries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM