当前位置:   article > 正文

SpringBoot3.0.1自动装配_springboot3.0 自动装配

springboot3.0 自动装配

SpringBoot的自动装配是SpringBoot的核心,而SpringBoot的启动类上的@SpringBootApplication注解中包括@SpringBootConfiguration、@EnableAutoConfiguration和@ComponentScan三个注解,其中@EnableAutoConfiguration就是自动装配的核心注解。

  1. @Target({ElementType.TYPE})
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Documented
  4. @Inherited
  5. @SpringBootConfiguration
  6. //自动装配的核心注解
  7. @EnableAutoConfiguration
  8. @ComponentScan(
  9. excludeFilters = {@Filter(
  10. type = FilterType.CUSTOM,
  11. classes = {TypeExcludeFilter.class}
  12. ), @Filter(
  13. type = FilterType.CUSTOM,
  14. classes = {AutoConfigurationExcludeFilter.class}
  15. )}
  16. )
  17. public @interface SpringBootApplication {
  18. }

对@EnableAutoConfiguration进行跟踪,@Import({AutoConfigurationImportSelector.class})就是负责自动装配。

  1. @Target({ElementType.TYPE})
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Documented
  4. @Inherited
  5. @AutoConfigurationPackage
  6. @Import({AutoConfigurationImportSelector.class})
  7. public @interface EnableAutoConfiguration {
  8. String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
  9. Class<?>[] exclude() default {};
  10. String[] excludeName() default {};
  11. }

跟踪AutoConfigurationImportSelector.class,其中有个getAutoConfigurationEntry方法,用于自动获取配置了类信息。

  1. protected AutoConfigurationImportSelector.AutoConfigurationEntry getAutoConfigurationEntry(AnnotationMetadata annotationMetadata) {
  2. if (!this.isEnabled(annotationMetadata)) {
  3. return EMPTY_ENTRY;
  4. } else {
  5. AnnotationAttributes attributes = this.getAttributes(annotationMetadata);
  6. List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes);
  7. configurations = this.removeDuplicates(configurations);
  8. Set<String> exclusions = this.getExclusions(annotationMetadata, attributes);
  9. this.checkExcludedClasses(configurations, exclusions);
  10. configurations.removeAll(exclusions);
  11. configurations = this.getConfigurationClassFilter().filter(configurations);
  12. this.fireAutoConfigurationImportEvents(configurations, exclusions);
  13. return new AutoConfigurationImportSelector.AutoConfigurationEntry(configurations, exclusions);
  14. }
  15. }

其中getCandidateConfigurations方法用于获取候选配置

  1. protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
  2. List<String> configurations = ImportCandidates.load(AutoConfiguration.class, this.getBeanClassLoader()).getCandidates();
  3. Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports. If you are using a custom packaging, make sure that file is correct.");
  4. return configurations;
  5. }

其中的ImportCandidates.load方法解析出配置文件的路径"META-INF/spring/%s.imports",也就是spring-boot-autoconfigure包下的META-INF下的spring中的org.springframework.boot.autoconfigure.AutoConfiguration.imports文件

文件中就包括了自动加载的类,在具体的装配时,会进行条件判断,符合了才会自动装配,也就是判断项目中是否真的引入了依赖。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/796515
推荐阅读
相关标签
  

闽ICP备14008679号