Spring Boot——用于构建微服务的基础框架
使用Spring框架时,会发现其配置文件比较复杂,许多内容经常都只是复制粘贴,比如通过Maven构建项目时,我们可以通过配置文件来自动导入依赖的jar包,但要使应用支持Spring,我们就要写不少代码,而且还要排查是否有遗漏的jar包,这不利于项目的快速构建和开发。spring boot的出现有效改善这种问题,它设计了大量的自动化配置来简化Spring原有样板化配置,使得开发者可以快速构建应用。
除了解决配置问题外,Spring Boot还通过一系列的Starter POMs的定义,让我们整合各项功能的时候,不需要在Maven的pom.xml中维护那些复杂的依赖关系,而是通过类似模块化的Starter模块定义来引用,使得依赖管理工作变得很简单。
没使用Spring Boot之前的pom.xml
只列出一个项目中的部分依赖
<dependencies>
...
...
...
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- JSP相关 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<scope>provided</scope>
</dependency>
...
...
</dependencies>
使用Spring Boot的pom.xml
web相关依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
同时,Spring Boot除了可以很好融入Docker之外,其自身也支持嵌入式的Tomcat,Jetty等容器。通过Spring Boot构建的应该不需要再安装Tomcat,将应用达成war包,然后把war包部署到Tomcat上这些操作,只需要将Spring Boot应用打成jar包,通过java -jar jar包 就能运行一个标准化的web应用了。
快速构建Spring Boot项目
通过官方的项目构建工具Spring Initializr生成一个Spring Boot的基础项目
根据需求选择不同的构建工具和版本,以及依赖项,Generate Project后,会生成一个压缩文件,解压后,通过Idea导入maven项目。目录结构如下: