一文道尽servlet spring springboot springcloud的区别

背景

最近在看java的框架,一直在spring和springcloud之间来回绕。为了弄清楚这几个框架之间的关系,专门写了每一个实例程序这样就比较清楚了。下面就把这几个概念详细介绍下

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是一套复杂的框架,里面有诸多组件。为了对比spring带来的变化,使用springMVC来实例。同样的对一个api的实现。spring的核心代码如下。

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

可以看到 使用spring后需要些的代码确实大幅度减少了。但是在使用时候会有复杂的配置。需要配置servlet.xml
比如

<?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

可以看到spring的配置相对比较复杂且繁琐,所以有了springboot框架来帮忙简化这一操作。同时,springboot在打包时也可以直接将tomcat打包进去,方便了部署。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 只需要写这些,不用再去配置servlet就可以达到同样的效果。

springcloud

springcloud 其实是提供了一整套微服务的解决方案。提到微服务可以看到springclcoud把一个个的程序都封装成了一个一个微服务。所以springcloud实际上可以认为是承载了诸多的springboot的应用解决方案。
这里举例子启动了一个 springcloud中的应用注册中心示例。

@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {

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

总结

基于此可以看到 servlet是spring的基础。
springboot是为了简化spring的配置,并封装成了微服务。
springcloud是一套微服务的解决方案。
具体代码示例 请看
https://github.com/ssdxiao/spring-learn

发布了6 篇原创文章 · 获赞 0 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/ssdxiao0/article/details/100156372