文章目录
Spring Boot启动器简介
Springboot包含许多启动项目并快速运行所需的依赖项,并且具有一组受支持的被管理的传递性依赖项,所有的正式starter都遵循类似的命名模式spring-boot-starter-*
。
starter启动器是一组方便的依赖项描述符,可以在pom中引入其依赖,免去了自己需要引用很多依赖类,并且SpringBoot会自动进行类的自动配置。例如,如果要使用SpringMVC开发web应用,可以在pom文件中包含spring-boot-starter-web
依赖项,如下:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
自定义springboot启动器
有时候,springboot官方提供的场景启动器(starter)并不能很好的满足我们的需求,这时候,我们可以考虑自行编写场景启动器,然后在common模块引入即可。
启动器包了一层自动配置类,自动配置类作用就是写@Bean注解,往容器里面配置Bean,然后Bean所需要的属性都来源于配置文件所映射的属性配置类。
springboot是按照这个模式开发启动器的,所以我们也这样操作,在需要的项目中引入启动器即可。
命名规约
-
spring官方:spring-boot-starter-xxx
-
自定义:xxx-spring–boot-starter
自定义starter步骤
1. 创建一个Spring boot项目
2. 导入pom
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
3. 编写配置类
然后新建一个配置类
package com.aliencat.springboot.starter.config;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
* mybatis plus分页插件配置
* @author wj
*/
@ConditionalOnClass(value = {PaginationInterceptor.class})
@EnableTransactionManagement
@Configuration
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
}
}
4. 在resources/META-INF目录下新建spring.factories文件
spring.factories:
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.aliencat.springboot.starter.config.MybatisPlusConfig
5. mvn install到本地仓库
6. 使用自定义starter
构建成功后,我们就可以再其他springboot项目的pom中引入自定义starter:
<dependency>
<groupId>com.aliencat.springboot.starter</groupId>
<artifactId>mystarter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
Spring Boot提供的启动器(starter)
如下所示:
名称 | 描述 | pom |
---|---|---|
spring-boot-starter | 核心Starter工具,包括自动配置支持,日志记录和YAML | pom |
spring-boot-starter-activemq | 使用Apache ActiveMQ的JMS消息传递Starter | pom |
spring-boot-starter-amqp | 使用Spring AMQP和Rabbit MQ的Starter | pom |
spring-boot-starter-aop | 使用Spring AOP和AspectJ进行面向方面编程的Starter | pom |
spring-boot-starter-artemis | 使用Apache Artemis的JMS消息传递Starter | pom |
spring-boot-starter-batch | 使用Spring Batch的Starter | pom |
spring-boot-starter-cache | 使用Spring Framework的缓存支持的Starter | pom |
spring-boot-starter-cloud-connectors | 使用Spring Cloud Connectors的Starter程序,可简化与Cloud Foundry和Heroku等云平台中服务的连接。不推荐使用Java CFEnv | pom |
spring-boot-starter-data-cassandra | 使用Cassandra分布式数据库和Spring Data Cassandra的Starter | pom |
spring-boot-starter-data-cassandra-reactive | 使用Cassandra分布式数据库和Spring Data Cassandra Reactive的Starter | pom |
spring-boot-starter-data-couchbase | 使用Couchbase面向文档的数据库和Spring Data Couchbase的Starter | pom |
spring-boot-starter-data-couchbase-reactive | 使用Couchbase面向文档的数据库和Spring Data Couchbase Reactive的Starter | pom |
spring-boot-starter-data-elasticsearch | 使用Elasticsearch搜索和分析引擎以及Spring Data Elasticsearch的Starter | pom |
spring-boot-starter-data-jdbc | 使用Spring Data JDBC的Starter | pom |
spring-boot-starter-data-jpa | 将Spring Data JPA与Hibernate结合使用的Starter | pom |
spring-boot-starter-data-ldap | 使用Spring Data LDAP的Starter | pom |
spring-boot-starter-data-mongodb | 使用MongoDB面向文档的数据库和Spring Data MongoDB的Starter | pom |
spring-boot-starter-data-mongodb-reactive | 使用MongoDB面向文档的数据库和Spring Data MongoDB Reactive的Starter | pom |
spring-boot-starter-data-neo4j | 使用Neo4j图形数据库和Spring Data Neo4j的Starter工具 | pom |
spring-boot-starter-data-redis | 使用Redis键值数据存储与Spring Data Redis和Lettuce客户端的Starter | pom |
spring-boot-starter-data-redis-reactive | 将Redis键值数据存储与Spring Data Redis Reacting和Lettuce客户端一起使用的Starter | pom |
spring-boot-starter-data-rest | 使用Spring Data REST在REST上公开Spring数据存储库的Starter | pom |
spring-boot-starter-data-solr | 将Apache Solr搜索平台与Spring Data Solr结合使用的Starter | pom |
spring-boot-starter-freemarker | 使用FreeMarker视图构建MVC Web应用程序的Starter | pom |
spring-boot-starter-groovy-templates | 使用Groovy模板视图构建MVC Web应用程序的Starter | pom |
spring-boot-starter-hateoas | 使用Spring MVC和Spring HATEOAS构建基于超媒体的RESTful Web应用程序的Starter | pom |
spring-boot-starter-integration | 使用Spring Integration的Starter | pom |
spring-boot-starter-jdbc | 结合使用JDBC和HikariCP连接池的Starter | pom |
spring-boot-starter-jersey | 使用JAX-RS和Jersey构建RESTful Web应用程序的Starter的替代品spring-boot-starter-web | pom |
spring-boot-starter-jooq | 使用jOOQ访问SQL数据库的Starter。替代spring-boot-starter-data-jpa 或spring-boot-starter-jdbc | pom |
spring-boot-starter-json | 读写JSONStarter | pom |
spring-boot-starter-jta-atomikos | 使用Atomikos的JTA交易Starter | pom |
spring-boot-starter-jta-bitronix | 使用Bitronix的JTA交易Starter | pom |
spring-boot-starter-mail | 使用Java Mail和Spring Framework的电子邮件发送支持的Starter | pom |
spring-boot-starter-mustache | 使用Mustache视图构建Web应用程序的Starter | pom |
spring-boot-starter-oauth2-client | 使用Spring Security的OAuth2 / OpenID Connect客户端功能的Starter | pom |
spring-boot-starter-oauth2-resource-server | 使用Spring Security的OAuth2资源服务器功能的Starter | pom |
spring-boot-starter-quartz | Starter使用Quartz Scheduler | pom |
spring-boot-starter-rsocket | 用于构建RSocket客户端和服务器的Starter程序。 | pom |
spring-boot-starter-security | 使用Spring Security的Starter | pom |
spring-boot-starter-test | 用于使用包括JUnit,Hamcrest和Mockito在内的库测试Spring Boot应用程序的Starter程序 | pom |
spring-boot-starter-thymeleaf | 使用Thymeleaf视图构建MVC Web应用程序的Starter | pom |
spring-boot-starter-validation | 初学者,可将Java Bean验证与Hibernate Validator结合使用 | pom |
spring-boot-starter-web | 使用Spring MVC构建Web(包括RESTful)应用程序的Starter程序。使用Tomcat作为默认的嵌入式容器 | pom |
spring-boot-starter-web-services | 使用Spring Web Services的Starter | pom |
spring-boot-starter-webflux | 使用Spring Framework的反应式Web支持构建WebFlux应用程序的Starter | pom |
spring-boot-starter-websocket | 使用Spring Framework的WebSocket支持构建WebSocket应用程序的Starter | pom |
spring-boot-starter-actuator | 使用Spring Boot的Actuator的Starter程序,该启动器提供了生产就绪功能,可帮助您监视和管理应用程序 | pom |
spring-boot-starter-jetty | 使用Jetty作为嵌入式servlet容器的Starter的替代品spring-boot-starter-tomcat | pom |
spring-boot-starter-log4j2 | 使用Log4j2进行日志记录的启动器的替代品spring-boot-starter-logging | pom |
spring-boot-starter-logging | 使用Logback进行日志记录的启动器。默认记录启动器 | pom |
spring-boot-starter-reactor-netty | Starter用于将Reactor Netty用作嵌入式反应式HTTP服务器。 | pom |
spring-boot-starter-tomcat | Starter用于将Tomcat用作嵌入式servlet容器。默认使用的servlet容器启动器spring-boot-starter-web | pom |
spring-boot-starter-undertow | 使用Undertow作为嵌入式servlet容器的Starter的替代品spring-boot-starter-tomcat | pom |