21.1.1 添加 Starter 依赖

有两种烤制蛋糕的方式,有热情的人会将面粉、鸡蛋、糖、发酵粉、盐、奶油、香草调料以及牛奶混合在一起,和成糊状。或者也可以购买预先打包好的蛋糕,它包含了所需的大部分原料,我们只需添加一些含水分的材料即可,如水、鸡蛋和植物油。

预先打包好的蛋糕将制作蛋糕过程中所需的各种材料集合在了一起,作为一项材料来使用,与之类似,Spring Boot Starter 将应用所需的各种依赖聚合成一项依赖。

为了阐述该功能,假设我们要从头开始编写一个新的 Spring 应用。这是一个 Web 项目,所以需要使用 Spring MVC。同时,还要有 REST API 将资源暴露为 JSON,所以在构建中需要包含 Jackson JSON 库。

因为应用需要使用 JDBC 从关系型数据库中存储和查询数据,因此我们希望确保包含了 Spring 的 JDBC 模块(为了使用 JdbcTemplate)和 Spring 的事务模块(为了使用声明式事务的支持)。对于数据库本身,H2 数据库是个不错的选择。

对了,我们还需要使用 Thymeleaf 来建立 Spring MVC 视图。

如果使用 Gradle 构建项目的话,在 build.gradle 中(至少)需要包含如下的依赖:

dependencies {
  compile("org.springframework:spring-web:4.0.6.RELEASE")
  compile("org.springframework:spring-webmvc:4.0.6.RELEASE")
  compile("com.fasterxml.jackson.core:jackson-databind:2.2.2")
  compile("org.springframework:spring-jdbc:4.0.6.RELEASE")
  compile("org.springframework:spring-tx:4.0.6.RELEASE")
  compile ("com.h2database:h2:1.3.174")
  compile("org.thymeleaf:thymeleaf-spring4:2.1.2.RELEASE")
}

幸好,Gradle 能够非常简洁地表达依赖。(为简单起见,我不再展现这个依赖列表在 Maven 的 pom.xml 文件是什么样子了。)即便如此,创建这个文件还是牵扯到许多的事情,而对它的维护则会更加麻烦。这些依赖之间是如何协作的呢?当应用程序不断地成长和演进,依赖管理将会变得更加具有挑战性。

但是,如果我们使用 Spring Boot Starer 所提供的预打包依赖的话,那么 Gradle 依赖列表能够更加简短一些:

dependencies {
  compile("org.springframework.boot:spring-boot-starter-web:1.1.4.RELEASE")
  compile("org.springframework.boot:spring-boot-starter-jdbc:1.1.4.RELEASE")
  compile ("com.h2database:h2:1.3.174")
  compile("org.thymeleaf:thymeleaf-spring4:2.1.2.RELEASE")
}

可以看到,Spring Boot 的 Web 和 JDBC Starter 取代了几个更加细粒度的依赖。我们依然还需要包含 H2 和 Thymeleaf 的依赖,不过其他的依赖都已经放到了 Starter 中。除了依赖列表更加简短,我们可以相信由 Starter 所提供的依赖版本能够互相兼容。

Spring Boot 提供了多个 Starter,Web 和 JDBC 只是其中的两个。表 21.1 列出了我在编写本章时,所有可用的 Starter。

Starter

所提供的依赖

spring-boot-starter-actuator

spring-boot-starter 、spring-boot-actuator 、spring-core

spring-boot-starter-amqp

spring-boot-starter 、spring-boot-rabbit 、spring-core 、spring-tx

spring-boot-starter- aop

spring-boot-starter 、spring-aop 、AspectJ Runtime 、AspectJ Weaver 、spring-core

spring-boot-starter- batch

spring-boot-starter 、HSQLDB 、spring-jdbc 、spring-batch-core 、spring-core

spring-boot-starter-elasticsearch

spring-boot-starter、 spring-data-elasticsearch、 spring-core 、 spring-tx

spring-boot-starter-gemfire

spring-boot-starter、 Gemfire、 spring-core、 spring-tx、spring-context、 spring-context-support、 spring-data-gemfire

spring-boot-starter- data-jpa

spring-boot-starter、 spring-boot-starter-jdbc、 spring-boot-starter- aop、 spring-core、 Hibernate EntityManager、 spring-orm 、 spring-data-jpa、 spring-aspects

spring-boot-starter-data-mongodb

spring-boot-starter、 MongoDB Java 驱动 、 spring-core、spring-tx、 spring-data-mongodb

spring-boot-starter-datarest

spring-boot-starter、 spring-boot-starter-web、 Jackson 注解、 Jackson 数据绑定 、 spring-core、 spring-tx、 spring-data-rest-webmvc

spring-boot-starter-datasolr

spring-boot-starter、 Solrj、 spring-core、 spring-tx、spring-data-solr、 Apache HTTP Mime

spring-boot-starter-freemarker

spring-boot-starter、 spring-boot-starter-web、 Freemarker、spring-core、 spring-context-support

spring-boot-starter-groovy-templates

spring-boot-starter、 spring-boot-starter-web、 Groovy、Groovy 模板、spring-core

spring-boot-starter-hornetq

spring-boot-starter、 spring-core、 spring-jms、 Hornet JMS Client

spring-boot-starter-integration

spring-boot-starter、 spring-aop、 spring-tx、 spring-web、spring-webmvc、 spring-integration-core、 spring-integration-file 、 spring-integration-http、 spring-integration-ip、spring-integration-stream

spring-boot-starter- jdbc

spring-boot-starter、 spring-jdbc 、tomcat-jdbc、 spring-tx

spring-boot-starter- jetty

jetty-webapp、 jetty-jsp

spring-boot-starter- log4j

jcl-over-slf4j、 jul-to-slf4j 、slf4j-log4j12、log4j

spring-boot-starter - logging

jcl-over-slf4j、 jul-to-slf4j 、log4j-over-slf4j、 logback-classic

spring-boot-starter-mobile

spring-boot-starter、 spring-boot-starter-web、 spring-mobile-device

spring-boot-starter- redis

spring-boot-starter、 spring-data-redis、 lettuce

spring-boot-starter-remote- shell

spring-boot-starter-actuator、 spring-context、 org.crashub.**

spring-boot-starter-security

spring-boot-starter、 spring-security-config、 spring-security- web、 spring-aop、 spring-beans、 spring-context、spring-core、 spring-expression、 spring-web

spring-boot-starter-social-facebook

spring-boot-starter、 spring-boot-starter-web、 spring-core、spring-social-config、 spring-social-core、 spring-social-web 、 spring-social-facebook

spring-boot-starter-social-twitter

spring-boot-starter、 spring-boot-starter-web、 spring-core、spring-social-config、 spring-social-core、 spring-social-web 、 spring-social-twitter

spring-boot-starter-social-linkedin

spring-boot-starter、 spring-boot-starter-web、 spring-core、spring-social-config、 spring-social-core、 spring-social-web 、 spring-social-linkedin

spring-boot-starter

spring-boot、 spring-boot-autoconfigure、 spring-boot-starter-logging

spring-boot-starter- test

spring-boot-starter-logging、 spring-boot、 junit、mockito-core 、 hamcrest-library、 spring-test

spring-boot-starter-thymeleaf

spring-boot-starter、 spring-boot-starter-web、 spring-core、thymeleaf-spring4、 thymeleaf-layout-dialect

spring-boot-starter-tomcat

tomcat-embed-core、 tomcat-embed-logging-juli

spring-boot-starter- web

spring-boot-starter、 spring-boot-starter-tomcat、 jackson-databind 、 spring-web、 spring-webmvc

spring-boot-starter-websocket

spring-boot-starter-web、 spring-websocket、 tomcat-embed-core 、 tomcat-embed-logging-juli

spring-boot-starter- ws

spring-boot-starter、 spring-boot-starter-web、 spring-core、spring-jms、 spring-oxm、 spring-ws-core、 spring-ws-support

如果查看这些 Starter 依赖的内部原理,你会发现 Starter 的工作方式也没有什么神秘之处。它使用了 Maven 和 Gradle 的依赖传递方案,Starter 在自己的 pom.xml 文件中声明了多个依赖。当我们将某一个 Starter 依赖添加到 Maven 或 Gradle 构建中的时候,Starter 的依赖将会自动地传递性解析。这些依赖本身可能也会有其他的依赖。一个 Starter 可能会传递性地引入几十个依赖。

需要注意,很多 Starter 引用了其他的 Starter。例如,mobile Starter 就引用了 Web Starter,而后者又引用了 Tomcat Starter。大多数的 Starter 都会引用 spring-boot-starter,它实际上是一个基础的 Starter(当然,它也依赖了 logging Starter)。依赖是传递性的,将 mobile Starter 添加为依赖之后,就相当于添加了它下面的所有 Starter。

Last updated