All the difference between servlet spring springboot springcloud

background

Recently looking at the framework of java, has been going back and forth between spring and springcloud. In order to clarify the relationship between these frameworks, each example program is written specifically so that it is clearer. The following introduces these concepts in detail

servlet

Servlet是指Java语言实现的一个接口。其实通俗讲,就是把url访问映射到相应的servlet类。最明显的使用就是要自己实现Servlet接口。

···
@WebServlet("/HelloServlet")
public class HelloServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public HelloServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 使用 GBK 设置中文正常显示
        response.setCharacterEncoding("GBK");
        response.getWriter().write("菜鸟教程:http://www.runoob.com");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }
}·

spring

Spring is a complex framework with many components. In order to compare the changes brought by spring, use springMVC as an example. The same for the realization of an api. The core code of spring is as follows.

@Controller
public class helloworld
{
    @RequestMapping("/click")
    public String hello()
    {
        System.out.println("hellowolrd");
        return "result";
    }
}

It can be seen that the code needed after using spring is indeed greatly reduced. However, there will be complicated configuration during use. Need to configure servlet.xml
such as

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

        <!-- 配置自动扫描的包 -->
        <mvc:annotation-driven/>
        <context:component-scan base-package="com.spring.handlers">
        <!--context:include-filter type="annotation" expression="com.spring.handlers.helloworld"/-->
        </context:component-scan>

        <!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 -->
       <!--prefix和suffix:查找视图页面的前缀和后缀(前缀[逻辑视图名]后缀), -->
    <!-- 比如传进来的逻辑视图名为result,则该该jsp视图页面应该存放在“/WEB-INF/result.jsp”  -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name = "prefix" value="/WEB-INF/"></property>
            <property name = "suffix" value = ".jsp"></property>
        </bean>
</beans>

springboot

It can be seen that the configuration of spring is relatively complicated and cumbersome, so there is a springboot framework to help simplify this operation. At the same time, springboot can also package tomcat directly when packaging, which facilitates deployment. The core code of springboot

/**
 *  * @SpringBootApplication:标注一个主程序类,用来标明这是一个Spring Boot应用
 *   */
@SpringBootApplication
public class SpringBootApplicationMain {
    public static void main(String[] args) {
     SpringApplication.run(SpringBootApplicationMain.class, args);
    }
}
@RestController
public class HelloWorldController {

    @RequestMapping("/hello")
    public String say(){
        return "Spring Boot";
    }
}

Springboot only needs to write these, you can achieve the same effect without having to configure the servlet.

leap cloud

springcloud actually provides a complete set of microservices solutions. When referring to microservices, you can see that springclcoud encapsulates each program into a microservice. So springcloud can actually be considered as an application solution that carries a lot of springboot.
The example here starts an example of an application registration center in springcloud.

@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class, args);
    }
}

to sum up

Based on this, we can see that servlet is the foundation of spring.
springboot is to simplify the configuration of spring and encapsulates it as a microservice.
springcloud is a set of solutions for microservices.
For specific code examples, please see
https://github.com/ssdxiao/spring-learn

Published 6 original articles · Likes0 · Visitors 10,000+

Guess you like

Origin blog.csdn.net/ssdxiao0/article/details/100156372