Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 368
Sathish Kumar k k Avatar asked Jul 10 '26 14:07

Sathish Kumar k k


1 Answers

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.

like image 118
ron Avatar answered Jul 12 '26 04:07

ron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!