在了解什么是 Starters 之前,我们先看看一个简单 Spring Boot 项目的 POM 依赖配置,如下:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
上面 POM 配置中,spring-boot-starter-web 和 spring-boot-starter-test 就是 Spring Boot 提供的两个 Starters。其中:
spring-boot-starter-web:用来支持 Spring Boot 进行 Web 开发
spring-boot-starter-test:用来支持 Srping Boot 单元测试
因此,Starters 是一个依赖描述符的集合,你可以将它包含进项目中,这样添加依赖就非常方便。你可以获取所有 Spring 及相关技术的一站式服务,而不需要翻阅示例代码,拷贝粘贴大量的依赖描述符。例如,如果你想使用 Spring 和 JPA 进行数据库访问,只需要在项目中包含 spring-boot-starter-data-jpa 依赖,然后你就可以开始了。 该 starters 包含很多搭建,快速运行项目所需的依赖,并提供一致的,可管理传递性的依赖集。
所有官方 starters 遵循相似的命名模式: spring-boot- starter-*
在这里 * 是一种特殊的应用程序类型。该命名结构旨在帮你找到需要的 starter。很多集成于 IDEs 中的 Maven 插件允许你通过名称name 搜索依赖。例如,使用相应的 Eclipse 或 STS 插件,你可以简单地在 POM 编辑器中按 ctrl-space,然后输入 “spring-boot-starter” 就可以获取一个完整列表。
注意:第三方 starters 不应该以 spring-boot 开头,因为它跟 Spring Boot 官方 artifacts 冲突。一个 acme 的第三方 starter 通常命名为 acme-spring-boot-starter 。
以下应用程序 starters 是 Spring Boot 在 org.springframework.boot group下提供的:
spring-boot-starter-test:用于测试 Spring Boot 应用,支持常用测试类库,包括 JUnit, Hamcrest 和 Mockito
spring-boot-starter-mobile:用于使用 Spring Mobile 开发 web 应用
spring-boot-starter-social-twitter:对使用 Spring Social Twitter 的支持
spring-boot-starter-cache:用于使用 Spring 框架的缓存支持
spring-boot-starter-activemq:用于使用 Apache ActiveMQ 实现 JMS 消息
spring-boot-starter-jta-atomikos:用于使用 Atomikos 实现 JTA 事务
spring-boot-starter-aop:用于使用 Spring AOP 和 AspectJ 实现面向切面编程
spring-boot-starter-web:用于使用 Spring MVC 构建 web 应用,包括 RESTful。Tomcat 是默认的内嵌容器
spring-boot-starter-data-elasticsearch:用于使用 Elasticsearch 搜索,分析引擎和 Spring Data Elasticsearch
spring-boot-starter-jdbc:对 JDBC 的支持(使用 Tomcat JDBC 连接池)
spring-boot-starter-batch:对 Spring Batch 的支持
spring-boot-starter-social-facebook:用于使用 Spring Social Facebook
spring-boot-starter-web-services:对 Spring Web 服务的支持
spring-boot-starter-jta-narayana Spring Boot Narayana JTA Starter
spring-boot-starter-thymeleaf:用于使用Thymeleaf模板引擎构建MVC web应用
spring-boot-starter-mail:用于使用Java Mail和Spring框架email发送支持
spring-boot-starter-jta-bitronix:用于使用Bitronix实现JTA事务
spring-boot-starter-data-mongodb:用于使用基于文档的数据库MongoDB和Spring Data MongoDB
spring-boot-starter-validation:用于使用Hibernate Validator实现Java Bean校验
spring-boot-starter-jooq:用于使用JOOQ访问SQL数据库,可使用spring-boot-starter-data-jpa或spring-boot-starter-jdbc替代
spring-boot-starter-redis:用于使用Spring Data Redis和Jedis客户端操作键-值存储的Redis,在1.4中已被spring-boot-starter-data-redis取代
spring-boot-starter-data-cassandra:用于使用分布式数据库Cassandra和Spring Data Cassandra
spring-boot-starter-hateoas:用于使用Spring MVC和Spring HATEOAS实现基于 超媒体的RESTful web应用
spring-boot-starter-integration:用于使用Spring Integration Pom spring-boot-starter-data-solr 通过Spring Data Solr使用Apache Solr搜索平台
spring-boot-starter-freemarker:用于使用FreeMarker模板引擎构建MVC web应用
spring-boot-starter-jersey:用于使用JAX-RS和Jersey构建RESTful web应用, 可使用spring-boot-starter-web替代
spring-boot-starter:核心 starter,包括自动配置支持,日志和YAML
spring-boot-starter-data-couchbase:用于使用基于文档的数据库Couchbase和Spring Data Couchbase
spring-boot-starter-artemis:使用Apache Artemis实现JMS消息
spring-boot-starter-cloud-connectors:对 Spring Cloud Connectors的支持,用于简化云平 台下(例如Cloud Foundry 和Heroku)服务的连接
spring-boot-starter-social-linkedin:用于使用 Spring Social LinkedIn
spring-boot-starter-velocity:用于使用 Velocity 模板引擎构建 MVC web 应用,从 1.4 版本过期
spring-boot-starter-data-rest:用于使用 Spring Data REST 暴露基于 REST 的 Spring Data 仓库
spring-boot-starter-groovy-templates:用于使用 Groovy 模板引擎构建 MVC web 应用
spring-boot-starter-amqp:用于使用 Spring AMQP 和 Rabbit MQ
spring-boot-starter-hornetq:用于使用 HornetQ 实现 JMS 消息,被 spring-boot-starter-artemis 取代
spring-boot-starter-ws:用于使用 Spring Web 服务,被 spring-boot-starter-web-services 取代
spring-boot-starter-security:对 Spring Security 的支持
spring-boot-starter-data-redis:用于使用 Spring Data Redis 和 Jedis 客户端操作键 —值数据存储 Redis
spring-boot-starter-websocket:用于使用 Spring 框架的 WebSocket 支持构建 WebSocket 应用
spring-boot-starter-mustache:用于使用 Mustache 模板引擎构建 MVC web 应用
spring-boot-starter-data-neo4j:用于使用图数据库 Neo4j 和 Spring Data Neo4j
spring-boot-starter-data-jpa:用于使用 Hibernate 实现 Spring Data JPA
除了应用程序starters,以下starters可用于添加production ready的功能:
spring-boot-starter-actuator:用于使用Spring Boot的Actuator,它提供了 production ready功能来帮助你监控和管理应用程序
spring-boot-starter-remote-shell:用于通过SSH,使用CRaSH远程shell监控,管理 你的应用
最后,Spring Boot还包含一些用于排除或交换某些特定技术方面的starters:
spring-boot-starter-undertow:用于使用Undertow作为内嵌servlet容器,可使:用spring-boot-starter-tomcat替代
spring-boot-starter-logging:用于使用Logback记录日志,默认的日志starter
spring-boot-starter-tomcat:用于使用Tomcat作为内嵌servlet容器,spring-boot-starter-web使用的默认servlet容器
spring-boot-starter-jetty:用于使用Jetty作为内嵌servlet容器,可使用spring-boot-starter-tomcat替代
spring-boot-starter-log4j2:用于使用Log4j2记录日志,可使用spring-boot-starter-logging代替