13.5. Starts
Starters是一个依赖描述符的集合,你可以将它包含进项目中,这样添加依赖就非常方便。你可以获取所有Spring及相关技术的一站式服务,而不需要翻阅示例代码,拷贝粘贴大量的依赖描述符。例如,如果你想使用Spring和JPA进行数据库访问,只需要在项目中包含 spring-boot-starter-data-jpa 依赖,然后你就可以开始了。该starters包含很多搭建,快速运行项目所需的依赖,并提供一致的,可管理传递性的依赖集。
第三方starters不应该以spring-boot
开头,因为它跟Spring Boot官方artifacts冲突。一个acme的第三方starter通常命名为acmespring-boot-starter
具体的依赖传递过程如下图所示:
首先,我们的项目继承了
spring-boot-starter-parent
,而spring-boot-starter-parent
项目继承了spring-boot-dependencies
,因此我们的项目可以使用这两个项目pom中的属性和依赖管理。
然后,spring-boot-starters
是所有starter类型项目的父项目,该项目继承了spring-boot-parent
,spring-boot-parent
继承了spring-boot-dependencies
,因此spring-boot-starters
项目可以使用依赖管理。
接着,spring-boot-starters
使用Maven 聚合的方式,聚合了诸如spring-boot-starter-xx
的模块(module),这些项目中定义了具体需要用到的依赖(jar包),但本身没有代码只有一个pom.xml,在dependence
里引入这些项目,就等于将它们pom定义里的依赖引入到当前我们的项目,从而又形成了依赖树
由上图可以看出,因为spring-boot-starter-xx
项目都经过严密的测试,因此其相互依赖版本是一致且安全的,spring-boot-starter依赖spring-boot依赖spring-core,而spring-boot-starter自身又依赖spring-core,但由于spring-core版本相同,因此不存在冲突(没有两个不同版本的相同的依赖jar包),这就是spring-boot-starter-xx
另一个好处了,可以放心使用
IDEA用户可以在maven插件中直接查看依赖树
使用Maven命令查看依赖树
# 查看依赖树
mvn dependency:tree -Dverbose
spring-boot-starter-xx
的模块也不需要指定<version>
,因为这个已经定义在它的父pomspring-boot-dependencies
里了
最后,我们在自己的项目里<dependences>
导入spring-boot-starter-xx
,就是将它代表的功能模块需要的依赖带入项目中,而不需要挨个复制具体的jar包,且不需要指定具体<version>
所谓Maven的聚合就是通过一个父项目,将所有子项目以module的方式组合在该父项目的POM下(该父项目只有一个pomx.xml),子项目继承该父项目,同时子项目间可以互相依赖
Table Spring Boot application starters
Name Description Pom Core starter, including auto-configuration support, logging and YAML
Starter for JMS messaging using Apache ActiveMQ
Starter for using Spring AMQP and Rabbit MQ
Starter for aspect-oriented programming with Spring AOP and AspectJ
Starter for JMS messaging using Apache Artemis
Starter for using Spring Batch
Starter for using Spring Framework’s caching support
Starter for using Spring Cloud Connectors which simplifies connecting to services in cloud platforms like Cloud Foundry and Heroku
Starter for using Cassandra distributed database and Spring Data Cassandra
Starter for using Couchbase document-oriented database and Spring Data Couchbase
Starter for using Elasticsearch search and analytics engine and Spring Data Elasticsearch
Starter for using GemFire distributed data store and Spring Data GemFire
Starter for using Spring Data JPA with Hibernate
Starter for using Spring Data LDAP
Starter for using MongoDB document-oriented database and Spring Data MongoDB
Starter for using Neo4j graph database and Spring Data Neo4j
Starter for using Redis key-value data store with Spring Data Redis and the Jedis client
Starter for exposing Spring Data repositories over REST using Spring Data REST
Starter for using the Apache Solr search platform with Spring Data Solr
Starter for building MVC web applications using FreeMarker views
Starter for building MVC web applications using Groovy Templates views
Starter for building hypermedia-based RESTful web application with Spring MVC and Spring HATEOAS
Starter for using Spring Integration
Starter for using JDBC with the Tomcat JDBC connection pool
Starter for building RESTful web applications using JAX-RS and Jersey. An alternative to
spring-boot-starter-web
Starter for using jOOQ to access SQL databases. An alternative to
spring-boot-starter-data-jpa
orspring-boot-starter-jdbc
Starter for JTA transactions using Atomikos
Starter for JTA transactions using Bitronix
Spring Boot Narayana JTA Starter
Starter for using Java Mail and Spring Framework’s email sending support
Starter for building web applications using Spring Mobile
Starter for building MVC web applications using Mustache views
Starter for using Spring Security
Starter for using Spring Social Facebook
Stater for using Spring Social LinkedIn
Starter for using Spring Social Twitter
Starter for testing Spring Boot applications with libraries including JUnit, Hamcrest and Mockito
Starter for building MVC web applications using Thymeleaf views
Starter for using Java Bean Validation with Hibernate Validator
Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container
Starter for using Spring Web Services
Starter for building WebSocket applications using Spring Framework’s WebSocket support