spring-boot 简介

spring-boot 是什么?

每次开始一个新的spring应用,少不了要细心准备各种文件和基础配置项,配置完还要进行测试,少不了的会有配置遗漏或错误。如果是老手,可能很快的看出问题,或者有保存自己的配置模板,复制一下改一改。但毕竟是有问题的,为什么创建一个spring应用这么麻烦,为什么不向其他语言和框架脚手架等工具借鉴一下呢?正因如此,spring社区也感受到了开发者的痛点,于是退出了spring-boot这个项目。

spring-boot,目标是减少创建spring应用的工作量,免除繁琐的XML配置而无需生成代码,遵循约定优于配置的哲学,还提供许多常用的特性和功能(如内置服务器、安全、健康检查等),使开发者能够快速的创建工业级配置的spring应用。让你专心做事,不再因为配置而分心烦躁。而且就算在命令行中,也可以迅速地 get things done。

github传送门

快速安装

首先当然,除了java环境,maven和gradle之一还是要装的,毕竟需要一种构建工具(这里使用maven),spring-boot提供了许多starter和samples进行参考,下面就来体验一下。

例:spring web starter

创建一个maven项目,并继承spring-boot-starter-parent的pom:

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.1.RELEASE</version>
</parent>

增加对starter-web的依赖:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

如果要用jetty而非tomcat,替换掉依赖即可:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

新建一个Controller:

@RestController
@EnableAutoConfiguration
public class SampleController {

@RequestMapping("/")
@ResponseBody
String home() {
return "Hello World!";
}

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

运行:

mvn spring-boot:run

这个是在命令行是使用maven运行,需要安装spring-boot插件(也可以不适用maven插件,运行main()方法亦可):

pom.xml

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

打开浏览器访问http://localhost:8080/,出现了熟悉的"Hello World!"。

从上面的sample可以感受到,starter-web为我们简化了许多操作,其实看一下当前的依赖tree就可以知道:

mvn dependency:tree > deps.txt

依赖树:

[INFO] --- maven-dependency-plugin:2.10:tree (default-cli) @ learning-starter-web ---
[INFO] org.niko.learn.spring.boot:learning-starter-web:jar:1.3.1.RELEASE
[INFO] \- org.springframework.boot:spring-boot-starter-web:jar:1.3.1.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter:jar:1.3.1.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot:jar:1.3.1.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-autoconfigure:jar:1.3.1.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-logging:jar:1.3.1.RELEASE:compile
[INFO] | | +- ch.qos.logback:logback-classic:jar:1.1.3:compile
[INFO] | | | +- ch.qos.logback:logback-core:jar:1.1.3:compile
[INFO] | | | \- org.slf4j:slf4j-api:jar:1.7.13:compile
[INFO] | | +- org.slf4j:jcl-over-slf4j:jar:1.7.13:compile
[INFO] | | +- org.slf4j:jul-to-slf4j:jar:1.7.13:compile
[INFO] | | \- org.slf4j:log4j-over-slf4j:jar:1.7.13:compile
[INFO] | +- org.springframework:spring-core:jar:4.2.4.RELEASE:compile
[INFO] | \- org.yaml:snakeyaml:jar:1.16:runtime
[INFO] +- org.springframework.boot:spring-boot-starter-tomcat:jar:1.3.1.RELEASE:compile
[INFO] | +- org.apache.tomcat.embed:tomcat-embed-core:jar:8.0.30:compile
[INFO] | +- org.apache.tomcat.embed:tomcat-embed-el:jar:8.0.30:compile
[INFO] | +- org.apache.tomcat.embed:tomcat-embed-logging-juli:jar:8.0.30:compile
[INFO] | \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.0.30:compile
[INFO] +- org.springframework.boot:spring-boot-starter-validation:jar:1.3.1.RELEASE:compile
[INFO] | \- org.hibernate:hibernate-validator:jar:5.2.2.Final:compile
[INFO] | +- Javax.validation:validation-api:jar:1.1.0.Final:compile
[INFO] | +- org.jboss.logging:jboss-logging:jar:3.3.0.Final:compile
[INFO] | \- com.fasterxml:classmate:jar:1.1.0:compile
[INFO] +- com.fasterxml.jackson.core:jackson-databind:jar:2.6.4:compile
[INFO] | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.6.4:compile
[INFO] | \- com.fasterxml.jackson.core:jackson-core:jar:2.6.4:compile
[INFO] +- org.springframework:spring-web:jar:4.2.4.RELEASE:compile
[INFO] | +- org.springframework:spring-aop:jar:4.2.4.RELEASE:compile
[INFO] | | \- aopalliance:aopalliance:jar:1.0:compile
[INFO] | +- org.springframework:spring-beans:jar:4.2.4.RELEASE:compile
[INFO] | \- org.springframework:spring-context:jar:4.2.4.RELEASE:compile
[INFO] \- org.springframework:spring-webmvc:jar:4.2.4.RELEASE:compile
[INFO] \- org.springframework:spring-expression:jar:4.2.4.RELEASE:compile

参考

【1】 https://docs.spring.io/spring-boot/docs/current/reference/html/getting-started-first-application.html