SpringBoot Starter原理(依赖引入,自动配置)

创建SpringBoot应用,我们在使用Web开发时,选择的是spring-boot-starter-web。starter是一种服务,使用某个功能的开发者不需要关注各种依赖库的处理,不需要具体的配置信息,由Spring Boot自动通过classpath路径下的类发现并加载需要的Bean。

原理

SpringBoot Starter原理

利用starter实现自动化配置只需要两个条件——maven依赖、配置文件。引入maven实质上就是导入jar包,spring-boot启动的时候会找到starter jar包中的resources/META-INF/spring.factories文件,根据spring.factories文件中的配置,找到需要自动配置的类。

配置类上面包括以下注解

@Configuration 表明是一个配置文件,被注解的类将成为一个bean配置类

@ConditionalOnClass 当classpath下发现该类的情况下进行自动配置

@ConditionalOnBean 当classpath下发现该类的情况下进行自动配置

@EnableConfigurationProperties 使@ConfigurationProperties注解生效

@AutoConfigureAfter 完成自动配置后实例化这个bean

依赖引入

spring-boot-starter 核心Spring Boot starter,包括自动配置支持,日志和YAML

spring-boot-starter-actuator 生产准备的特性,用于帮我们监控和管理应用

spring-boot-starter-amqp 对”高级消息队列协议”的支持,通过spring-rabbit实现

spring-boot-starter-aop 对面向切面编程的支持,包括spring-aop和AspectJ

spring-boot-starter-batch 对Spring Batch的支持,包括HSQLDB数据库

spring-boot-starter-cloud-connectors 对Spring Cloud Connectors的支持,简化在云平台下(例如,Cloud Foundry 和Heroku)服务的连接

spring-boot-starter-data-elasticsearch 对Elasticsearch搜索和分析引擎的支持,包括spring-data-elasticsearch

spring-boot-starter-data-gemfire 对GemFire分布式数据存储的支持,包括spring-data-gemfire

spring-boot-starter-data-jpa 对”Java持久化API”的支持,包括spring-data-jpa,spring-orm和Hibernate

spring-boot-starter-data-mongodb 对MongoDB NOSQL数据库的支持,包括spring-data-mongodb

spring-boot-starter-data-rest 对通过REST暴露Spring Data仓库的支持,通过spring-data-rest-webmvc实现

spring-boot-starter-data-solr 对Apache Solr搜索平台的支持,包括spring-data-solr

spring-boot-starter-freemarker 对FreeMarker模板引擎的支持

spring-boot-starter-groovy-templates 对Groovy模板引擎的支持

spring-boot-starter-hateoas 对基于HATEOAS的RESTful服务的支持,通过spring-hateoas实现

spring-boot-starter-hornetq 对”Java消息服务API”的支持,通过HornetQ实现

spring-boot-starter-integration 对普通spring-integration模块的支持

spring-boot-starter-jdbc 对JDBC数据库的支持

spring-boot-starter-jersey 对Jersey RESTful Web服务框架的支持

spring-boot-starter-jta-atomikos 对JTA分布式事务的支持,通过Atomikos实现

spring-boot-starter-jta-bitronix 对JTA分布式事务的支持,通过Bitronix实现

spring-boot-starter-mail 对javax.mail的支持

spring-boot-starter-mobile 对spring-mobile的支持

spring-boot-starter-mustache 对Mustache模板引擎的支持

spring-boot-starter-redis 对REDIS键值数据存储的支持,包括spring-redis

spring-boot-starter-security 对spring-security的支持

spring-boot-starter-social-facebook 对spring-social-facebook的支持

spring-boot-starter-social-linkedin 对spring-social-linkedin的支持

spring-boot-starter-social-twitter 对spring-social-twitter的支持

spring-boot-starter-test 对常用测试依赖的支持,包括JUnit, Hamcrest和Mockito,还有spring-test模块

spring-boot-starter-thymeleaf 对Thymeleaf模板引擎的支持,包括和Spring的集成

spring-boot-starter-velocity 对Velocity模板引擎的支持

spring-boot-starter-web 对全栈web开发的支持, 包括Tomcat和spring-webmvc

spring-boot-starter-websocket 对WebSocket开发的支持

spring-boot-starter-ws 对Spring Web服务的支持

依赖spring-boot-starter-web,springboot会根据需要自动引入jar包。

自动配置

我们引入starter的依赖,会将自动配置的类的jar引入。@SpringBootApplication的注解中有一个是@EnableAutoConfiguration注解,这个注解有一个@Import({EnableAutoConfigurationImportSelector.class}),EnableAutoConfigurationImportSelector内部则是使用了SpringFactoriesLoader.loadFactoryNames方法进行扫描具有META-INF/spring.factories文件的jar包。而自动配置类的jar的是有一个META-INF/spring.factories文件内容如下:

应用在启动时就会加载spring-boot-autoconfigure的jar包下面的META-INF/spring.factories文件中定义的autoconfiguration类。将configuration类中定义的bean加入spring到容器中。就相当于加载之前我们自己配置组件的xml文件。而现在SpringBoot自己定义了一个默认的值,然后直接加载进入了Spring容器。

SpringBoot提供的自动配置依赖模块都以spring-boot-starter-为命名前缀,并且这些依赖都在org.springframework.boot下。 所有的spring-boot-starter都有约定俗成的默认配置,但允许调整这些配置调整默认的行为。

版权声明:本文为JAVASCHOOL原创文章,未经本站允许不得转载。