VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > Java教程 >
  • SpringBoot自动装配原理

1、从@SpringBootApplication启动注解入手

  • 源码
  • 
    
    @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 {}; //直接根据class类扫描 @AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses") Class<?>[] scanBasePackageClasses() default {}; }
     
     
  • 初看@SpringBootApplication有很多的注解组成,其实归纳就是一个"三体"结构,重要的只有三个Annotation:
  • @SpringBootConfiguration(@SpringBootConfiguration实质就是一个@Configuration) @EnableAutoConfiguration @ComponentScan
  • 也就是说我们在开发的时候,加上上面的上个注解会等同于加上@SpringBootApplication注解

 

  

(1)@SpringBootConfiguration注解

  这个注解实际上就是代表了一个配置类,相当于一个beans.xml文件

(2)@ComponentScan

  @ComponentScan的功能其实就是自动扫描并加载符合条件的组件或bean定义,最终将这些bean定义加载到容器中

3)@EnableAutoConfiguration

  在spring中有关于@Enablexxx的注解是开启某一项功能的注解,比如@EnableScheduling表示开启spring的定时任务。其原理是借助@Import的帮助,

  将所有符合自动配置条件的bean定义加载到Ioc容器

  • EnableAutoConfiguration代表开启springboot的自动装配
  • 
    
    @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @AutoConfigurationPackage @Import(AutoConfigurationImportSelector.class) public @interface EnableAutoConfiguration { String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration"; //按类型排序不需要自动装配的类 Class<?>[] exclude() default {}; //按名称排除不需要自动装配的类 String[] excludeName() default {}; }
  • 从源码中可以知道,最关键的要属@Import(EnableAutoConfigurationImportSelector.class),借助EnableAutoConfigurationImportSelector,
    @EnableAutoConfiguration可以帮助SpringBoot应用将所有符合条件的@Configuration配置都加载到当前SpringBoot创建并使用的IoC容器。
    同时借助于Spring框架原有的一个工具类:SpringFactoriesLoader,@EnableAutoConfiguration就可以实现智能的自动配置。
     
    
    
    //从这里可以看出该类实现很多的xxxAware和DeferredImportSelector,所有的aware都优先于selectImports //方法执行,也就是说selectImports方法最后执行,那么在它执行的时候所有需要的资源都已经获取到了 public class AutoConfigurationImportSelector implements DeferredImportSelector, BeanClassLoaderAware, ResourceLoaderAware, BeanFactoryAware, EnvironmentAware, Ordered { ... public String[] selectImports(AnnotationMetadata annotationMetadata) { if (!this.isEnabled(annotationMetadata)) { return NO_IMPORTS; } else { //1加载META-INF/spring-autoconfigure-metadata.properties文件 AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader.loadMetadata(this.beanClassLoader); //2获取注解的属性及其值(PS:注解指的是@EnableAutoConfiguration注解) AnnotationAttributes attributes = this.getAttributes(annotationMetadata); //3.在classpath下所有的META-INF/spring.factories文件中查找org.springframework.boot.autoconfigure.EnableAutoConfiguration的值,并将其封装到一个List中返回 List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes); //4.对上一步返回的List中的元素去重、排序 configurations = this.removeDuplicates(configurations); //5.依据第2步中获取的属性值排除一些特定的类 Set<String> exclusions = this.getExclusions(annotationMetadata, attributes); //6对上一步中所得到的List进行过滤,过滤的依据是条件匹配。这里用到的过滤器是 //org.springframework.boot.autoconfigure.condition.OnClassCondition最终返回的是一个ConditionOutcome[] //数组。(PS:很多类都是依赖于其它的类的,当有某个类时才会装配,所以这次过滤的就是根据是否有某个 //class进而决定是否装配的。这些类所依赖的类都写在META-INF/spring-autoconfigure-metadata.properties文件里) this.checkExcludedClasses(configurations, exclusions); configurations.removeAll(exclusions); configurations = this.filter(configurations, autoConfigurationMetadata); this.fireAutoConfigurationImportEvents(configurations, exclusions); return StringUtils.toStringArray(configurations); } } protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) { List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader()); Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct."); return configurations; } ... }
     
     
    SpringFactoriesLoader中加载配置,SpringFactoriesLoader属于Spring框架私有的一种扩展方案,其主要功能就是从指定的配置文件META-INF/spring.factories加载配置,即根据@EnableAutoConfiguration的完整类名org.springframework.boot.autoconfigure.EnableAutoConfiguration作为查找的Key,获取对应的一组@Configuration类
     
    
    
    public abstract class SpringFactoriesLoader { public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories"; private static Map<String, List<String>> loadSpringFactories(
     
     

    总结

    @EnableAutoConfiguration作用就是从classpath中搜寻所有的META-INF/spring.factories配置文件,

    并将其中org.springframework.boot.autoconfigure.EnableutoConfiguration对应的配置项通过反射(Java Refletion)实例化为对应的标注了@Configuration的JavaConfig形式的IoC容器配置类,

    然后汇总为一个并加载到IoC容器。这些功能配置类要生效的话,会去classpath中找是否有该类的依赖类(也就是pom.xml必须有对应功能的jar包才行)并且配置类里面注入了默认属性值类,

    功能类可以引用并赋默认值。生成功能类的原则是自定义优先,没有自定义时才会使用自动装配类。

     

    所以功能类能生效需要的条件:(1)spring.factories里面有这个类的配置类(一个配置类可以创建多个围绕该功能的依赖类)(2)pom.xml里面需要有对应的jar包

    
    
     

__EOF__

 
本文作者 本文链接:https://www.cnblogs.com/diaoyulin/p/15086685.html


相关教程