摘要:簡單的說,就通過具體就是類作為入口,配合注解實現了自動配置。注解讓自動配置生效。接下來,從開始分析。這個同名方法中,創建了一個對象并調用了對象的成員方法。是類的對象,的方法中有解析的主要邏輯。分別對注解注解注解注解注解注解進行解析。
Springboot使用的版本是2.1.6.RELEASE。
簡單的說,Springboot就通過BeanFactoryPostProcessor(具體就是ConfigurationClassPostProcessor類)作為入口,配合注解實現了自動配置。
可以先去先了解下BeanFactoryPostProcessor(bean工廠后處理器)和AnnotatedElementUtils(注解工具類)有助于理解
首先,看一下Springboot的啟動代碼。
@RestController @SpringBootApplication public class Springboot216DemoApp { @RequestMapping("/") public String home() { return "Hello World!"; } public static void main(String[] args) { SpringApplication.run(Springboot216DemoApp.class); } }
上面的代碼中:
main()是Springboot應用的啟動。
@SpringBootApplication注解讓自動配置生效。
接下來,從SpringApplication.run(Springboot216DemoApp.class)開始分析。
public static ConfigurableApplicationContext run(Class> primarySource, String... args) { return run(new Class>[] { primarySource }, args); }
SpringApplication.run()是靜態方法,有兩個參數primarySource和args,primarySource一般傳遞main()方法
所在的類,args可以不傳。run()方法實際調用了另一個同名方法。
public static ConfigurableApplicationContext run(Class>[] primarySources, String[] args) { return new SpringApplication(primarySources).run(args); }
這個同名run()方法中,創建了一個SpringApplication對象并調用了SpringApplication對象的成員run()方法。
這里有三run()方法不要混淆了。
new SpringApplication(primarySources)中做了一些初始化的工作先不看了,先看SpringApplication對象
的成員run()方法。
public ConfigurableApplicationContext run(String... args) { StopWatch stopWatch = new StopWatch(); stopWatch.start(); ConfigurableApplicationContext context = null; CollectionexceptionReporters = new ArrayList<>(); configureHeadlessProperty(); SpringApplicationRunListeners listeners = getRunListeners(args); listeners.starting(); try { ApplicationArguments applicationArguments = new DefaultApplicationArguments(args); ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments); configureIgnoreBeanInfo(environment); Banner printedBanner = printBanner(environment); context = createApplicationContext(); exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[] { ConfigurableApplicationContext.class }, context); prepareContext(context, environment, listeners, applicationArguments, printedBanner); refreshContext(context); afterRefresh(context, applicationArguments); stopWatch.stop(); if (this.logStartupInfo) { new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch); } listeners.started(context); callRunners(context, applicationArguments); } ...... }
成員run()方法中
context = createApplicationContext();
refreshContext(context);
這兩行代碼是關鍵。
context = createApplicationContext()
protected ConfigurableApplicationContext createApplicationContext() { Class> contextClass = this.applicationContextClass; if (contextClass == null) { try { switch (this.webApplicationType) { case SERVLET: contextClass = Class.forName(DEFAULT_SERVLET_WEB_CONTEXT_CLASS); break; case REACTIVE: contextClass = Class.forName(DEFAULT_REACTIVE_WEB_CONTEXT_CLASS); break; default: contextClass = Class.forName(DEFAULT_CONTEXT_CLASS); } } catch (ClassNotFoundException ex) { throw new IllegalStateException( "Unable create a default ApplicationContext, " + "please specify an ApplicationContextClass", ex); } } return (ConfigurableApplicationContext) BeanUtils.instantiateClass(contextClass); }
createApplicationContext()方法中會根據不同的應用類型創建不同的Spring的ApplicationContext,
我們這個是servlet應用所以創建的是AnnotationConfigServletWebServerApplicationContext。
public AnnotationConfigServletWebServerApplicationContext() { this.reader = new AnnotatedBeanDefinitionReader(this); this.scanner = new ClassPathBeanDefinitionScanner(this); }
AnnotationConfigServletWebServerApplicationContext構造方法中,初始化了成員對象reader,reader
是AnnotatedBeanDefinitionReader類的對象。
public AnnotatedBeanDefinitionReader(BeanDefinitionRegistry registry, Environment environment) { Assert.notNull(registry, "BeanDefinitionRegistry must not be null"); Assert.notNull(environment, "Environment must not be null"); this.registry = registry; this.conditionEvaluator = new ConditionEvaluator(registry, environment, null); AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry); } public static void registerAnnotationConfigProcessors(BeanDefinitionRegistry registry) { registerAnnotationConfigProcessors(registry, null); } public static SetregisterAnnotationConfigProcessors( BeanDefinitionRegistry registry, @Nullable Object source) { DefaultListableBeanFactory beanFactory = unwrapDefaultListableBeanFactory(registry); if (beanFactory != null) { if (!(beanFactory.getDependencyComparator() instanceof AnnotationAwareOrderComparator)) { beanFactory.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE); } if (!(beanFactory.getAutowireCandidateResolver() instanceof ContextAnnotationAutowireCandidateResolver)) { beanFactory.setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver()); } } Set beanDefs = new LinkedHashSet<>(8); if (!registry.containsBeanDefinition(CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME)) { RootBeanDefinition def = new RootBeanDefinition(ConfigurationClassPostProcessor.class); def.setSource(source); beanDefs.add(registerPostProcessor(registry, def, CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME)); } ...... }
上面是創建AnnotatedBeanDefinitionReader對象的代碼,其中向Spring的ApplicationContext(即Registry)中注冊了
ConfigurationClassPostProcessor類的BeanDefinition,ConfigurationClassPostProcessor就是用來處理
Springboot的配置注解的,ConfigurationClassPostProcessor是一個BeanFactoryPostProcessor(bean工廠后處理器)。
refreshContext(context)
private void refreshContext(ConfigurableApplicationContext context) { refresh(context); if (this.registerShutdownHook) { try { context.registerShutdownHook(); } catch (AccessControlException ex) { // Not allowed in some environments. } } } protected void refresh(ApplicationContext applicationContext) { Assert.isInstanceOf(AbstractApplicationContext.class, applicationContext); ((AbstractApplicationContext) applicationContext).refresh(); } public void refresh() throws BeansException, IllegalStateException { synchronized (this.startupShutdownMonitor) { // Prepare this context for refreshing. prepareRefresh(); // Tell the subclass to refresh the internal bean factory. ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory(); // Prepare the bean factory for use in this context. prepareBeanFactory(beanFactory); try { // Allows post-processing of the bean factory in context subclasses. postProcessBeanFactory(beanFactory); // Invoke factory processors registered as beans in the context. invokeBeanFactoryPostProcessors(beanFactory); ...... }
refreshContext()方法最后調用到AbstractApplicationContext的refresh()方法,refresh()方法中的
invokeBeanFactoryPostProcessors()方法是用來調用bean工廠后處理器,前面的ConfigurationClassPostProcessor
就是在這里起作用的。
invokeBeanFactoryPostProcessors()方法中會對BeanFactoryPostProcessor分類然后分別調用,這里就不細說了。
直接看ConfigurationClassPostProcessor中的代碼。
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) { int registryId = System.identityHashCode(registry); if (this.registriesPostProcessed.contains(registryId)) { throw new IllegalStateException( "postProcessBeanDefinitionRegistry already called on this post-processor against " + registry); } if (this.factoriesPostProcessed.contains(registryId)) { throw new IllegalStateException( "postProcessBeanFactory already called on this post-processor against " + registry); } this.registriesPostProcessed.add(registryId); processConfigBeanDefinitions(registry); } public void processConfigBeanDefinitions(BeanDefinitionRegistry registry) { ListconfigCandidates = new ArrayList<>(); String[] candidateNames = registry.getBeanDefinitionNames(); for (String beanName : candidateNames) { BeanDefinition beanDef = registry.getBeanDefinition(beanName); if (ConfigurationClassUtils.isFullConfigurationClass(beanDef) || ConfigurationClassUtils.isLiteConfigurationClass(beanDef)) { if (logger.isDebugEnabled()) { logger.debug("Bean definition has already been processed as a configuration class: " + beanDef); } } else if (ConfigurationClassUtils.checkConfigurationClassCandidate(beanDef, this.metadataReaderFactory)) { configCandidates.add(new BeanDefinitionHolder(beanDef, beanName)); } } // Return immediately if no @Configuration classes were found if (configCandidates.isEmpty()) { return; } // Sort by previously determined @Order value, if applicable configCandidates.sort((bd1, bd2) -> { int i1 = ConfigurationClassUtils.getOrder(bd1.getBeanDefinition()); int i2 = ConfigurationClassUtils.getOrder(bd2.getBeanDefinition()); return Integer.compare(i1, i2); }); // Detect any custom bean name generation strategy supplied through the enclosing application context SingletonBeanRegistry sbr = null; if (registry instanceof SingletonBeanRegistry) { sbr = (SingletonBeanRegistry) registry; if (!this.localBeanNameGeneratorSet) { BeanNameGenerator generator = (BeanNameGenerator) sbr.getSingleton(CONFIGURATION_BEAN_NAME_GENERATOR); if (generator != null) { this.componentScanBeanNameGenerator = generator; this.importBeanNameGenerator = generator; } } } if (this.environment == null) { this.environment = new StandardEnvironment(); } // Parse each @Configuration class ConfigurationClassParser parser = new ConfigurationClassParser( this.metadataReaderFactory, this.problemReporter, this.environment, this.resourceLoader, this.componentScanBeanNameGenerator, registry); Set candidates = new LinkedHashSet<>(configCandidates); Set alreadyParsed = new HashSet<>(configCandidates.size()); do { parser.parse(candidates); parser.validate(); Set configClasses = new LinkedHashSet<>(parser.getConfigurationClasses()); configClasses.removeAll(alreadyParsed); // Read the model and create bean definitions based on its content if (this.reader == null) { this.reader = new ConfigurationClassBeanDefinitionReader( registry, this.sourceExtractor, this.resourceLoader, this.environment, this.importBeanNameGenerator, parser.getImportRegistry()); } this.reader.loadBeanDefinitions(configClasses); alreadyParsed.addAll(configClasses); candidates.clear(); if (registry.getBeanDefinitionCount() > candidateNames.length) { String[] newCandidateNames = registry.getBeanDefinitionNames(); Set oldCandidateNames = new HashSet<>(Arrays.asList(candidateNames)); Set alreadyParsedClasses = new HashSet<>(); for (ConfigurationClass configurationClass : alreadyParsed) { alreadyParsedClasses.add(configurationClass.getMetadata().getClassName()); } for (String candidateName : newCandidateNames) { if (!oldCandidateNames.contains(candidateName)) { BeanDefinition bd = registry.getBeanDefinition(candidateName); if (ConfigurationClassUtils.checkConfigurationClassCandidate(bd, this.metadataReaderFactory) && !alreadyParsedClasses.contains(bd.getBeanClassName())) { candidates.add(new BeanDefinitionHolder(bd, candidateName)); } } } candidateNames = newCandidateNames; } } while (!candidates.isEmpty()); // Register the ImportRegistry as a bean in order to support ImportAware @Configuration classes if (sbr != null && !sbr.containsSingleton(IMPORT_REGISTRY_BEAN_NAME)) { sbr.registerSingleton(IMPORT_REGISTRY_BEAN_NAME, parser.getImportRegistry()); } if (this.metadataReaderFactory instanceof CachingMetadataReaderFactory) { // Clear cache in externally provided MetadataReaderFactory; this is a no-op // for a shared cache since it"ll be cleared by the ApplicationContext. ((CachingMetadataReaderFactory) this.metadataReaderFactory).clearCache(); } }
processConfigBeanDefinitions()方法中String[] candidateNames = registry.getBeanDefinitionNames()先從ApplicationContext
中取出所有的BeanDefinition,然后循環遍歷出有@Configuration注解的BeanDefinition,這里用到了AnnotatedElementUtils類的
isAnnotated()方法,可以遞歸的方式去找一個注解。如:@SpringBootApplication注解上還注解,會一直往里面找。注解上還有注解的方式
在Spring中很常見,Spring經常利用這一特性。
@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 { } @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Configuration public @interface SpringBootConfiguration { } @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @AutoConfigurationPackage @Import(AutoConfigurationImportSelector.class) public @interface EnableAutoConfiguration { } @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @Import(AutoConfigurationPackages.Registrar.class) public @interface AutoConfigurationPackage { }
找出有@Configuration注解的BeanDefinition后,開始解析解析這些配置。
parser.parse()方法就是來解析配置的。parser是ConfigurationClassParser類的對象,
ConfigurationClassParser的doProcessConfigurationClass()方法中有解析的主要邏輯。
分別對@Component注解、@PropertySource注解、@ComponentScan注解、@Import注解、@ImportResource注解、
@Bean注解進行解析。
spring.factories中的EnableAutoConfiguration類(各種自動配置類)就是通過@Import(AutoConfigurationImportSelector.class)
和@Import(AutoConfigurationPackages.Registrar.class)實現的。
protected final SourceClass doProcessConfigurationClass(ConfigurationClass configClass, SourceClass sourceClass) throws IOException { if (configClass.getMetadata().isAnnotated(Component.class.getName())) { // Recursively process any member (nested) classes first processMemberClasses(configClass, sourceClass); } // Process any @PropertySource annotations for (AnnotationAttributes propertySource : AnnotationConfigUtils.attributesForRepeatable( sourceClass.getMetadata(), PropertySources.class, org.springframework.context.annotation.PropertySource.class)) { if (this.environment instanceof ConfigurableEnvironment) { processPropertySource(propertySource); } else { logger.info("Ignoring @PropertySource annotation on [" + sourceClass.getMetadata().getClassName() + "]. Reason: Environment must implement ConfigurableEnvironment"); } } // Process any @ComponentScan annotations SetcomponentScans = AnnotationConfigUtils.attributesForRepeatable( sourceClass.getMetadata(), ComponentScans.class, ComponentScan.class); if (!componentScans.isEmpty() && !this.conditionEvaluator.shouldSkip(sourceClass.getMetadata(), ConfigurationPhase.REGISTER_BEAN)) { for (AnnotationAttributes componentScan : componentScans) { // The config class is annotated with @ComponentScan -> perform the scan immediately Set scannedBeanDefinitions = this.componentScanParser.parse(componentScan, sourceClass.getMetadata().getClassName()); // Check the set of scanned definitions for any further config classes and parse recursively if needed for (BeanDefinitionHolder holder : scannedBeanDefinitions) { BeanDefinition bdCand = holder.getBeanDefinition().getOriginatingBeanDefinition(); if (bdCand == null) { bdCand = holder.getBeanDefinition(); } if (ConfigurationClassUtils.checkConfigurationClassCandidate(bdCand, this.metadataReaderFactory)) { parse(bdCand.getBeanClassName(), holder.getBeanName()); } } } } // Process any @Import annotations processImports(configClass, sourceClass, getImports(sourceClass), true); // Process any @ImportResource annotations AnnotationAttributes importResource = AnnotationConfigUtils.attributesFor(sourceClass.getMetadata(), ImportResource.class); if (importResource != null) { String[] resources = importResource.getStringArray("locations"); Class extends BeanDefinitionReader> readerClass = importResource.getClass("reader"); for (String resource : resources) { String resolvedResource = this.environment.resolveRequiredPlaceholders(resource); configClass.addImportedResource(resolvedResource, readerClass); } } // Process individual @Bean methods Set beanMethods = retrieveBeanMethodMetadata(sourceClass); for (MethodMetadata methodMetadata : beanMethods) { configClass.addBeanMethod(new BeanMethod(methodMetadata, configClass)); } // Process default methods on interfaces processInterfaces(configClass, sourceClass); // Process superclass, if any if (sourceClass.getMetadata().hasSuperClass()) { String superclass = sourceClass.getMetadata().getSuperClassName(); if (superclass != null && !superclass.startsWith("java") && !this.knownSuperclasses.containsKey(superclass)) { this.knownSuperclasses.put(superclass, configClass); // Superclass found, return its annotation metadata and recurse return sourceClass.getSuperClass(); } } // No superclass -> processing is complete return null; }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/75619.html
摘要:啟動原理和執行原理分析一的啟動原理我們打開,注意看下面兩個依賴我們第一步是繼承了父項目,然后在添加啟動器的依賴,項目就會自動給我們導入關于項目所需要的配置和。 上一篇我們看到,我們很輕松的完成了項目的構建,那么SpringBoot是如何做到的呢,在使用的使用又有哪些通用配置和注意事項呢? 其實SpringBoot給我們做了大量的自動配置功能,我們只需要引入對應的啟動器就可以直接使用,作...
摘要:學習日記簡單的小程序配置引入依賴編寫代碼最后通過這兩個簡單的配置,就可以使用瀏覽器訪問訪問到這個頁面了分析和主要的作用是告知渲染結果直接返回給調用者。數據相當于就是在中作為路由功能的注解。 SpringBoot學習日記 簡單的Hello Word小程序 配置pom引入依賴 org.springframework.boot spring-boot-s...
摘要:前言用過的肯定很熟悉,它其中有個重要的特性,就是自動配置平時習慣的一些設置的配置作為默認配置。提倡無配置文件的理念,使用生成的應用完全不會生成任何配置代碼與配置文件。 前言 用過springboot的肯定很熟悉,它其中有個重要的特性,就是自動配置(平時習慣的一些設置的配置作為默認配置)。springboot提倡無XML配置文件的理念,使用springboot生成的應用完全不會生成任何配...
摘要:用于主類上最最最核心的注解,表示這是一個項目,用于開啟的各項能力。下面我們來分析一下這個注解的組成以及作用通過上面的代碼我們可以看出來是一個組合注解,主要由和這三個注解組成的。通過源碼可以看出也是一個組合注解。 ??SpringBoot項目一般都會有Application的入口類,入口類中會有main方法,這是一個標準的java應用程序的入口方法。@SpringBootApplicat...
摘要:所以,所謂的自動裝配,實際上就是如何自動將裝載到容器中來。實際上在版本中,模塊驅動注解的出現,已經有了一定的自動裝配的雛形,而真正能夠實現這一機制,還是在版本中,條件注解的出現。,我們來看一下的自動裝配是怎么一回事。在前面的分析中,Spring Framework一直在致力于解決一個問題,就是如何讓bean的管理變得更簡單,如何讓開發者盡可能的少關注一些基礎化的bean的配置,從而實現自動裝...
閱讀 1299·2021-11-15 11:37
閱讀 3495·2021-11-11 16:55
閱讀 1741·2021-08-25 09:39
閱讀 3207·2019-08-30 15:44
閱讀 1729·2019-08-29 12:52
閱讀 1397·2019-08-29 11:10
閱讀 3230·2019-08-26 11:32
閱讀 3216·2019-08-26 10:16