启动器
springboot项目会有很多spring-boot-starter打头的引用,这些引用是干什么用的,都有哪些,带着这些问题去官网看了一下官方文档,现将结果整理一下。
引用 | 说明 |
spring-boot-starter | 核心启动器,包括自动配置支持,日志记录和YAML |
spring-boot-starter-activemq | 使用Apache ActiveMQ的消息队列 |
spring-boot-starter-amqp | 使用Spring AMQP和Rabbit MQ消息队列 |
spring-boot-starter-aop | 使用SpringAOP和AspectJ进行切面编程包 |
spring-boot-starter-artemis | 使用Apache Artemis的消息队列 |
spring-boot-starter-batch | 使用轻量级的大数据量的并行处理(批处理)框架SpringBatch |
spring-boot-starter-cache | Spring缓存框架 |
spring-boot-starter-cloud-connectors | Spring Cloud Connectors,云连接器,便于PaaS应用在各种平台上连接到后端像数据库和消息经纪服务 |
spring-boot-starter-data-cassandra | NoSQL数据库cassandra |
spring-boot-starter-data-cassandra-reactive | NoSQL数据库cassandra响应式编程 |
spring-boot-starter-data-couchbase | NoSQL数据库Couchbase |
spring-boot-starter-data-couchbase-reactive | NoSQL数据库Couchbase响应式编程 |
spring-boot-starter-data-elasticsearch | 使用Elasticsearch搜索和分析引擎 |
spring-boot-starter-data-jdbc | 使用jdbc连接数据库进行数据支持久化 |
spring-boot-starter-data-jpa | 使用jpa连接数据库进行数据支持久化 |
spring-boot-starter-data-ldap | 使用ldap目录服务 |
spring-boot-starter-data-mongodb | 使用NoSQL数据库mongodb |
spring-boot-starter-data-mongodb-reactive | 使用NoSQL数据库mongodb响应式编程 |
spring-boot-starter-data-neo4j | 使用NoSQL数据库neo4j |
spring-boot-starter-data-redis | 使用NoSQL数据库redis |
spring-boot-starter-data-redis-reactive | 使用NoSQL数据库redis响应式编程 |
spring-boot-starter-data-rest | rest风格使用springData |
spring-boot-starter-data-solr | 使用solr搜索和分析引擎 |
spring-boot-starter-freemarker | 使用FreeMarker视图构建MVC Web应用程序 |
spring-boot-starter-groovy-templates | 使用Groovy模板视图构建MVC Web应用程序 |
spring-boot-starter-hateoas | 使用SpringMVC和SpringHATEOAS构建基于超媒体的RESTful Web应用程序 |
spring-boot-starter-integration | 使用SpringIntegration,类似消息队列,用于企业集成的框架 |
spring-boot-starter-jdbc | 使用jdbc连接池 |
spring-boot-starter-jersey | 使用jersey构建基于超媒体的RESTful Web应用程序 |
spring-boot-starter-jooq | 使用jooq数据库ORM框架 |
spring-boot-starter-json | 支持json读写 |
spring-boot-starter-jta-atomikos | 使用atomikos解决分布式事务 |
spring-boot-starter-jta-bitronix | 使用bitronix解决分布式事务 |
spring-boot-starter-mail | 使用Java Mail发送邮件 |
spring-boot-starter-mustache | 使用mustache轻量级模板语言视图构建MVC Web应用程序 |
spring-boot-starter-oauth2-client | 使用oauth2授权框架客户端 |
spring-boot-starter-oauth2-resource-server | 使用oauth2授权框架服务端 |
spring-boot-starter-quartz | 使用quartz任务调度框架 |
spring-boot-starter-security | 使用security安全框架 |
spring-boot-starter-test | 使用单元测试包含JUnit、 Hamcrest和 Mockito |
spring-boot-starter-thymeleaf | 使用thymeleaf模板语言视图构建MVC Web应用程序 |
spring-boot-starter-validation | 使用基于Hibernate Validator的类数据校验 |
spring-boot-starter-web | 构建网站开发,包括RESTful,Spring MVC,使用Tomcat作为默认嵌入容器 |
spring-boot-starter-web-services | 使用WebServices |
spring-boot-starter-webflux | 使用WebFlux响应式编程,与springMVC功能类似 |
spring-boot-starter-websocket | 使用websocket通讯服务 |
spring-boot-starter-actuator | 使用actuator监控应用健康 |
spring-boot-starter-jetty | 使用jetty作为嵌入式容器 |
spring-boot-starter-log4j2 | 使用log4j2记录日志 |
spring-boot-starter-logging | 使用logging记录日志 默认 |
spring-boot-starter-reactor-netty | 使用netty作为嵌入式容器 |
spring-boot-starter-tomcat | 使用tomcat作为嵌入式容器 默认 |
spring-boot-starter-undertow | 使用undertow |
启动机制
Springboot 启动时主要做3件事:
1) 整合第三方依赖 => maven 加载依赖 (pom.xml)
2) 集成SpringMVC
3) 启动Tomcat
@SpringBootApplication = @SpringBootConfiguration + @ComponentScan + @EnableAutoConfiguration
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
excludeFilters = {@Filter(
type = FilterType.CUSTOM,
classes = {TypeExcludeFilter.class}
), @Filter(
type = FilterType.CUSTOM,
classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
@AliasFor(
annotation = EnableAutoConfiguration.class
)
Class<?>[] exclude() default {};
@AliasFor(
annotation = EnableAutoConfiguration.class
)
String[] excludeName() default {};
@AliasFor(
annotation = ComponentScan.class,
attribute = "basePackages"
)
String[] scanBasePackages() default {};
@AliasFor(
annotation = ComponentScan.class,
attribute = "basePackageClasses"
)
Class<?>[] scanBasePackageClasses() default {};
}
@SpringBootConfiguration
@SpringBootApplication is a @Configuration, which is a configuration class in Spring container.
@EnableAutoConfiguration
@EnableAutoConfiguration will import @EnableWebMvc to start tomcat.
org.springframework.boot.autocnfigure.web.servlet.WebMvcAutoConfiguraion
@Bean
@ConditionOnClass(name = "org.apache.catalina.startup.Tomcat") // 内嵌Tomcat
public TomcatServiceFactoryCustomizer tomcatServiceFactoryCustomizer (ServiceProperties serviceProperties) {
return new TomcatServiceFactoryCustomizer (serviceProperties);
}
...
start tomcat
Spring Boot默认启动嵌入式tomcat容器,如果要改变启动jetty,需要去pom文件中配置。
@SprngBootApplication -> @SpringBootConfiguration -> @EnableAutoConfiguration -> @Import({AutoConfigurationImportSelector.class})
-> AutoConfigurationImportSelector (spring.factories)
-> [org.springframework.boot:spring-boot-autoconfigure:2.2.2RELEASE] - spring.factories
import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryConfiguration.**EmbeddedTomcat**;
-> EmbeddedTomcat -> TomcatServletWebServerFactory(Spring管理)
-> WebServer (getWebServer)-> TomcatWebServer -> this.tomcat.start()