前言
使用Spring Boot做后台项目开发也快半年了,由于之前有过基于Spring开发的项目经验,相比之下觉得Spring Boot就是天堂,开箱即用来形容是绝不为过的。在没有接触Spring Boot 之前,以为Spring Boot 是一个新的框架体系。正好Spring Boot出现先的时候,也是微服务特别火的时候,大家不约而同把Spring Boot 和微服务等同起来了,但其实并不是如此,那么到底什么是Spring Boot ? 基于Spring Boot 又是如何开发的呢 ? 带着这两个问题,大家继续往下看。
Spring Boot 特点
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.
- Absolutely no code generation and no requirement for XML configuration.
上述是从Spring Boot官方文档上摘录的,简述言之Spring Boot框架的目的就是开箱即用、去除配置。Spring Boot并不是重复去造就一个轮子,它的出现是为Spring的开发带来一种全新的体验,从而大大的降低Spring框架的使用门槛。
下面主要从几个特性介绍Spring Boot是如何简化开发的。
Spring Boot启动器
使用过Spring Boot的同学都知道,快速编写一个Spring Boot的Web Demo需要在pom文件中加入如下:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
Spring Boot 提供了很多像spring-boot-starter-web这样的启动器,名称都是spring-boot-starter-* 。
截图部分来自Spring Boot官网,只是Spring Boot启动器中小部分,更多启动器可以到官网上查找。
启动器为何物? 以spring-boot-starter-web 这个web开发用到的启动器为例。
从上图可以看出spring-boot-starter-web.jar包里面没有任何代码,对没有任何代码。只有一个pom文件。what ? 其中包中pom.xml的内容如下所示:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
</dependencies>