Loading

Spring Boot 简介

对于做 Java 开发的程序员,Spring 是一个绕不开的框架。现在几乎所有的 Java 项目都会使用 Spring 作为容器。但是大概两三年前有一个叫做 Spring Boot的“家伙”横空出世,其风头好像一时盖过了 Spring。所有基于 Spring 开发的新项目都转向使用 Spring Boot 进行开发了。那么 Spring Boot 究竟是一个什么框架?和 Spring 又有什么关系?本篇博客就来对 Spring Boot 做一个简单介绍。

Spring Boot 简介

我们先来看下 Spring Boot 官网对这个框架的介绍:

Spring Boot makes it easy to create stand-alone, production-grade Spring-based Applications that you can run. We take an opinionated view of the Spring platform and third-party libraries, so that you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration. You can use Spring Boot to create Java applications that can be started by using java -jar or more traditional war deployments. We also provide a command line tool that runs “spring scripts”.
Our primary goals are:
• Provide a radically faster and widely accessible getting-started experience for all Spring development.
• Be opinionated out of the box but get out of the way quickly as requirements start to diverge from the defaults.
• Provide a range of non-functional features that are common to large classes of projects (such as embedded servers, security, metrics, health checks, and externalized configuration).
• Absolutely no code generation and no requirement for XML configuration.

上面介绍的大概意思是:使用 Spring Boot 可以非常方便地创建生产级别的 Spring 应用;Spring Boot 提供了很多默认配置,但是程序员也可以非常方便的提供自己的配置;Spring Boot 可以大大减少甚至不使用传统 Spring 项目中的 XML 配置文件。

自己总结下:Spring Boot 其实并不是一个新的 Spring 框架,它进行了很多自动配置,目的是让用户能迅速开发基于 Spring 的应用,尽可能减少繁琐的Spring 配置,提升开发者的开发体验和开发效率。

Spring Boot 进行自动配置的原理是:根据开发者添加的 jar 包依赖(classpath 中的类),自动配置相应的 Bean,从而大大减少开发者手动的配置。比如说开发者添加了spring-boot-starter-web这个依赖,那么 Spring Boot 就认为你是要开发 Spring MVC 应用,它会自动帮你配置 DispatcherServlet、HandlerMapping 和 HandlerMethod 等一系列 Spring MVC 的核心组件。开发者就不需要自己在进行配置了。

Spring Boot 另外一个方便的地方是内嵌了 Servlet 容器,我们开发完之后可以直接将应用打成一个可执行的 Jar 包,而不是打成 war 包再部署到自己安装的 Servlet 容器中去。

下面我们用一个 Spring MVC 应用的列子,看看 Spring Boot 到底能给我们带来哪些方便。

使用 Spring Boot 开发项目

  1. 传统的开发流程

我们使用传统的 Spring 开发一个 Spring MVC 项目大概要做下面几个步骤:

  • 添加一些列 Spring 的核心依赖,添加 Spring MVC 依赖;
  • 配置 web.xml 配置文件,主要是配置 ContextLoaderListener 和 DispatcherServlet;
  • 进行 Spring MVC 相关的配置,主要是<mvc:default-servlet-handler/><mvc:annotation-driven/>等相关配置;
  • 日志相关配置;
  • 编写相关的 Controller;
  • 打成 war 包,安装 Tomcat,并部署 war 运行。

对于新手来说,上面的流程步骤还是很多的,很有可能在某一步出错。特别是好多刚刚接触Spring的同学对于第一步中要添加哪些依赖根本就“傻傻分不清楚”,比较影响学习的积极性。对于老手来说,上面的步骤可能都是一些重复性的操作,比较“浪费时间”。因此需要Spring Boot来解决这个问题。

  1. Spring Boot开发流程

使用Spring Boot开发Spring MVC我们需要下面几步:

  • 添加依赖

    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
     </dependency>
    
  • 编写主启动类

  • 编写相关的Controller

  • 直接打成可执行Jar包运行。

我们可以发现使用Spring Boot真的可以减少很多流程,而且大大减少自己配置出错的概率。

简单总结

Spring Boot是一个能够帮助开发者迅速开发Spring应用框架。它大大减少了传统Spring应用中的配置文件,提升开发效率和开发体验。同时Spring Boot还提了内嵌式的Servlet容器,可以将应用打成可执行Jar包直接运行。

一些问题

使用Spring Boot的确可以提升开发效率。但是它封装了太多细节,对于初学者来说Spring Boot也可能是“噩梦”。出了问题不知从何查起。所以我觉得对于框架这种东西我们还是要知道他们的原理性的知识,这样使用起来才能更加得心应手。

如果你是刚刚接触Spring Boot下面这些问题可以过段时间再考虑,先将Spring Boot用熟练再说。但如果你已经使用Spring Boot有段时间了,下面这些问题还是建议自己去琢磨琢磨:

  • Spring Boot的启动流程是怎样的;
  • Spring Boot中的自动配置是怎么生效的;
  • 内嵌的Servlet容器(比如Tomcat)是什么时候加载的;
  • 打成的可执行Jar包是怎么执行的。

当然上面只是我现在能想到的一些问题,自己在学习过程中可以去不断发现和总结。

公众号推荐

欢迎大家关注我的微信公众号「程序员自由之路」

posted @ 2019-08-22 16:20  程序员自由之路  阅读(561)  评论(0编辑  收藏  举报