springboot自动装配

tech2022-08-10  152

装载谁

@SpringBootApplication 

    @SpringBootConfiguration,@EnableAutoConfiguration,@ComponentScan

@EnableAutoConfiguration开启自动装配

    在classpath下找到all配置的bean进行装配,据若干conditional定制规则初始化

@Import(EnableAutoConfigurationImportSelector.class)1.5中过时,查看父类AutoConfigurationImportSelector public class AutoConfigurationImportSelector implements DeferredImportSelector, BeanClassLoaderAware, ResourceLoaderAware, BeanFactoryAware, EnvironmentAware, Ordere

首先实现了DeferredImportSelector接口,继承importSelector:接口上有很多书名,大家去看源码吧,deferred延迟的意思,该类延期导入、所有configuration都处理过后才执行

public interface ImportSelector { /** * Select and return the names of which class(es) should be imported based on * the {@link AnnotationMetadata} of the importing @{@link Configuration} class. */ String[] selectImports(AnnotationMetadata importingClassMetadata); }

 

 

在AutoConfigurationImportSelector中实现方法为:简单说一下主体方法

meta-inf/spring-autoconfigure-metadata.properties读取元数据及相关属性,调用getCandidateConfigurations读取meta-inf/spring.factories下的enableAutoConfiguration配置,排除过滤,得到相应装配的类,最后让配置

@Override public String[] selectImports(AnnotationMetadata annotationMetadata) { //是否自动装配,不是返回 if (!isEnabled(annotationMetadata)) { return NO_IMPORTS; } try { //从META-INF/spring-autoconfigure-metadata.properties读取元数据与元数据的相关属性 AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader .loadMetadata(this.beanClassLoader); AnnotationAttributes attributes = getAttributes(annotationMetadata); //借助SpringFactoryiesLoader读取META-INF/spring.factories下的EnableAutoConfiguration的配置 List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes); //排除 与过滤 configurations = removeDuplicates(configurations); configurations = sort(configurations, autoConfigurationMetadata); Set<String> exclusions = getExclusions(annotationMetadata, attributes); checkExcludedClasses(configurations, exclusions); configurations.removeAll(exclusions); configurations = filter(configurations, autoConfigurationMetadata); //所有配置在META-INF/spring.factories下的AutoConfigurationImportListener执行AutoConfigurationImportEvent事件fireAutoConfigurationImportEvents(configurations, exclusions); return configurations.toArray(new String[configurations.size()]); }catch (IOException ex) { throw new IllegalStateException(ex); } }

 

何时进行自动装配

1、AbstractApplicationContext的refresh方法(这个方法真的是博大精深)字字玑珠,我们先把眼光移步到:

// Invoke factory processors registered as beans in the context. invokeBeanFactoryPostProcessors(beanFactory);

 处理BeanFactoryPostProcessor,

PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors()); //看过源码你会知道,BeanFactoryPostProcessor是一个函数式接口 public List<BeanFactoryPostProcessor> getBeanFactoryPostProcessors() { return this.beanFactoryPostProcessors; }

 

 

ConfigurationClassPostProcessor 这个类主要是处理@Configuration,我要说一看就知道是不是有些不讲道理,debug一下就知道了,实现了BeanDefinitionRegistryPostProcessor,实现了

public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)

解析了每一个@ConfigurationClass类ConfigurationClassParser

最新回复(0)