2.1 父依赖: starter-parent
helloworld
查看依赖: 项目依赖了 spring-boot-stater-parent
:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
</dependency>
spring-boot-stater-parent
点进去, 里面还依赖了个parent
:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.3.2.RELEASE</version>
</parent>
spring-boot-dependencies
再点进去:
一大堆 properties
一大堆 dependencies
可见:
spring-boot-dependencies 才是springboot 所有依赖真正管理者;
2.2 导入的依赖: starter-web
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
点进去: spring-boot-starter-web帮我们导入了web项目所依赖的依赖项:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.3.2.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
<version>2.3.2.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.3.2.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.8.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.8.RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
spring-boot-starter
spring-boot-starter-json
spring-boot-starter-tomcat
spring-web
spring-webmvc
starter: spring boot帮助管理依赖资源;
spring提供了一系列starter: mail/web/jpa/mq…
2.3 @SpringBootApplication注解
注解类:
//....SpringBootApplication...
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration // 1
@EnableAutoConfiguration // 2
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
.....
//....SpringBootConfiguration...
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration // 3
public @interface SpringBootConfiguration {
.....
//....Configuration...
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component // 4
public @interface Configuration {
主要注解类:
2.4 SpringBootApplication之@SpringBootConfiguration
SpringBootConfiguration
可以看到, 有 Configuration
所以:
2.4.1 声明Application类为一个配置类
SpringBootApplication -> SpringBootConfiguration -> Configuration -> Component
注解了 @SpringBootApplication 就有了 @SpringBootConfiguration;
就有了 @Configuration 的功能; 即:此类是配置类!
就有了 @Component 的功能;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration // 继承了Component
public @interface SpringBootConfiguration {
上面说明: HelloApplication.java注解了@SpringBootApplication 它就是一个配置类, 可以被 AnnotationConfigApplicationContext
加载扫描起来!
2.5 SpringBootApplication之@EnableAutoConfiguration
按需自动注入spring.factories组件
2.5.1 EnableAutoConfiguration注解原理
路径: org.springframework.boot.autoconfigure.EnableAutoConfiguration
此类所属jar包
spring-boot-autoconfigure.jar
目录下有META-INF/spring.factories
文件, 内容如下: 可以这么理解: 下面所有类都是配置类, 作用都类似于写一个@Configuration
的类, 在其中初始化相应所需要的的@Bean
;# Initializers org.springframework.context.ApplicationContextInitializer=\ org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer,\ org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener # Application Listeners org.springframework.context.ApplicationListener=\ org.springframework.boot.autoconfigure.BackgroundPreinitializer # Auto Configuration Import Listeners org.springframework.boot.autoconfigure.AutoConfigurationImportListener=\ org.springframework.boot.autoconfigure.condition.ConditionEvaluationReportAutoConfigurationImportListener # Auto Configuration Import Filters org.springframework.boot.autoconfigure.AutoConfigurationImportFilter=\ org.springframework.boot.autoconfigure.condition.OnBeanCondition,\ org.springframework.boot.autoconfigure.condition.OnClassCondition,\ org.springframework.boot.autoconfigure.condition.OnWebApplicationCondition # Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\ org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\ org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\ ............省略100多个.... # Failure analyzers org.springframework.boot.diagnostics.FailureAnalyzer=\ org.springframework.boot.autoconfigure.data.redis.RedisUrlSyntaxFailureAnalyzer,\ org.springframework.boot.autoconfigure.diagnostics.analyzer.NoSuchBeanDefinitionFailureAnalyzer,\ org.springframework.boot.autoconfigure.flyway.FlywayMigrationScriptMissingFailureAnalyzer,\ org.springframework.boot.autoconfigure.jdbc.DataSourceBeanCreationFailureAnalyzer,\ org.springframework.boot.autoconfigure.jdbc.HikariDriverConfigurationFailureAnalyzer,\ org.springframework.boot.autoconfigure.r2dbc.ConnectionFactoryBeanCreationFailureAnalyzer,\ org.springframework.boot.autoconfigure.session.NonUniqueSessionRepositoryFailureAnalyzer # Template availability providers org.springframework.boot.autoconfigure.template.TemplateAvailabilityProvider=\ org.springframework.boot.autoconfigure.freemarker.FreeMarkerTemplateAvailabilityProvider,\ org.springframework.boot.autoconfigure.mustache.MustacheTemplateAvailabilityProvider,\ org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAvailabilityProvider,\ org.springframework.boot.autoconfigure.thymeleaf.ThymeleafTemplateAvailabilityProvider,\ org.springframework.boot.autoconfigure.web.servlet.JspTemplateAvailabilityProvider
2.5.2 EnableAutoConfiguration的继承关系
//....EnableAutoConfiguration...
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage // 5
@Import(AutoConfigurationImportSelector.class) // 6
public @interface EnableAutoConfiguration {}
//....AutoConfigurationPackage...
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import(AutoConfigurationPackages.Registrar.class) // 7
public @interface AutoConfigurationPackage {}
// ...Registrar...
// ...ImportBeanDefinitionRegistrar...
/**
* {@link ImportBeanDefinitionRegistrar} to store the base package from the importing
* configuration.
*/
static class Registrar implements ImportBeanDefinitionRegistrar, DeterminableImports {
@Override
public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
register(registry, new PackageImports(metadata).getPackageNames().toArray(new String[0]));
}
@Override
public Set<Object> determineImports(AnnotationMetadata metadata) {
return Collections.singleton(new PackageImports(metadata));
}
}
// ImportBeanDefinitionRegistrar 可以在spring容器bean初始化的时候注入组件到容器中
// 6 处是一个 ImportSelector
// 7 处是一个ImportBeanDefinitionRegistrar
这两处都是往容器中注册bean, 具体参见笔记:
spring注解驱动开发-(5) 向Spring容器中注册组件的方法
ImportBeanDefinitionRegistrar 可以在spring容器bean初始化的时候注入组件到容器中: 在这里打断点可以看到: 会把 com.niewj.springboot 包目录注册进去; 这样, 此包下所有子包中的spring注解的类就都会被扫描, 并初始化注册到spring容器的上下文;
这样的话, spring容器在启动容器的时候, 就会按需加载 spring-boot-autoconfigure.jar包下的META-INF/spring.factories
中配置的加载类; 达到自动配置注入bean到容器中的效果(目的就是方便开发, 不需要可怜的程序员一个一个写@Configuration的类, 然后自己写@Bean的方法注册组件) 这种机制就是 SPI
深入理解SPI机制
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 hi@niewj.com