上一节:SpringBoot开发笔记-(1) springboot简介和helloworldjava
helloworld
查看依赖: 项目依赖了 spring-boot-stater-parent
:程序员
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> </dependency>
spring-boot-stater-parent
点进去, 里面还依赖了个parent
:web
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.3.2.RELEASE</version> </parent>
spring-boot-dependencies
再点进去:redis
一大堆 properties
spring
一大堆 dependencies
json
可见:segmentfault
spring-boot-dependencies 才是springboot 全部依赖真正管理者;
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
点进去: spring-boot-starter-web帮咱们导入了web项目所依赖的依赖项:tomcat
<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帮助管理依赖资源;springboot
spring提供了一系列starter: mail/web/jpa/mq...session
注解类:
//....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 {
主要注解类:
SpringBootConfiguration
能够看到, 有 Configuration
因此:
SpringBootApplication -> SpringBootConfiguration -> Configuration -> Component
注解了 @SpringBootApplication 就有了 @SpringBootConfiguration;就有了 @Configuration 的功能; 即:此类是配置类!
就有了 @Component 的功能;
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Configuration // 继承了Component public @interface SpringBootConfiguration {
上面说明: HelloApplication.java注解了@SpringBootApplication 它就是一个配置类, 能够被 AnnotationConfigApplicationContext
加载扫描起来!
按需自动注入spring.factories组件
- 路径: 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
//....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机制